我有一个名为“构建你自己的电脑”的项目,它的结构如下:
build-your-own-computer
├── computer
│ ├── arithmetic
│ ├── __init__.py
│ ├── logic
│ └── memory
├── README.md
├── setup.py
├── setup.py~
├── solutions
│ ├── arithmetic
│ │ ├── half_adder.py
│ │ ├── __init__.py
│ │ └── __init__.py~
│ ├── __init__.py
│ ├── __init__.py~
│ ├── logic
│ │ ├── _and.py
│ │ ├── __init__.py
│ │ ├── __init__.py~
│ │ ├── _not.py
│ │ ├── _or.py
│ │ └── xor.py
│ └── memory
│ └── __init__.py
└── tests
├── arithmetic
│ └── test_half_adder.py
├── logic
│ ├── test_and.py
│ ├── test_not.py
│ ├── test_or.py
│ └── test_xor.py
└── memory我的目标是能够使用pip安装这个项目/包,然后在我的系统上的任何地方使用它。我想导入这样的包/模块:
from byoc.solutions.logic import _and
from byoc.computer.arithmetic import half_adder据我所知,build-your-own-computer本身可以被认为是包,build-your-own-computer\computer是子包,build-your-own-computer\computer\logic\是子包。除了init's和setup.py之外,所有的文件都是模块。这是正确的吗?上面的导入方案是否符合这个项目结构?
所有__init__.py文件都是空的。
setup.py包含以下内容:
from setuptools import setup, find_packages
setup(
name='byoc',
packages=find_packages()
)当我使用pip安装它,然后尝试导入子模块时,我会遇到以下问题:
>>> from byoc.solutions.logic import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'byoc.solutions'我可以没有错误地导入byoc。
我显然做错了什么,但是什么?
发布于 2020-04-20 20:43:39
您可以通过setuptools检查这里建议的布局。同时,这里也是一个很好的指南。这是我对你的布局的建议:
build-your-own-computer
├── README.md
├── setup.py
├── setup.cfg
├── src
│ └── byoc
│ ├── __init__.py
│ ├── computer
│ │ ├── arithmetic
│ │ ├── __init__.py
│ │ ├── logic
│ │ └── memory
│ └── solutions
│ ├── arithmetic
│ │ ├── half_adder.py
│ │ └── __init__.py
│ ├── __init__.py
│ ├── logic
│ │ ├── _and.py
│ │ ├── __init__.py
│ │ ├── __init__.py~
│ │ ├── _not.py
│ │ ├── _or.py
│ │ └── xor.py
│ └── memory
│ └── __init__.py
└── tests
├── arithmetic
│ └── test_half_adder.py
├── logic
│ ├── test_and.py
│ ├── test_not.py
│ ├── test_or.py
│ └── test_xor.py
└── memory然后你的setup.py应该是:
from setuptools import setup, find_packages
setup(
name='byoc',
packages=find_packages('src/')
package_dir={'': 'src/'},
)这样,您就可以在任何地方导入它,例如:
from byoc.solutions import logichttps://stackoverflow.com/questions/61194959
复制相似问题