我正在尝试用Python制作一个包。我有以下文件和目录结构:
.
├── ds
│ ├── __init__.py
│ ├── __main__.py
│ ├── package_a
│ │ ├── __init__.py
│ │ └── package_a_b
│ │ └── __init__.py
│ └── settings.py
├── install.sh
├── LICENSE
├── Manifest.in
├── README.md
└── setup.py
代码如下:
ds/__init__.py
空文件
ds/__main__.py
from package_a import name_a
from package_a.package_a_b import name_a_b
from settings import config
def main():
print(name_a)
print(config)
ds/package_a/__init__.py
name_a = 'name_a'
ds/package_a_b/__init__.py
name_a_b = 'name_a_b'
setup.py
import pathlib
from setuptools import setup
HERE = pathlib.Path(__file__).parent
README = (HERE / "README.md").read_text()
setup(
name="ds",
version="2.0.0",
long_description=README,
long_description_content_type="text/markdown",
license="MIT",
classifiers=[
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
],
packages=["ds"],
#packages=find_packages(exclude=("tests",)),
include_package_data=True,
entry_points={
"console_scripts": [
"ds=ds.__main__:main",
]
},
)
为了按照以下步骤安装
rm -rf dist
pip uninstall ds
python setup.py bdist_wheel
pip install dist/ds-2.0.0-py3-none-any.whl --force-reinstall
ds
问题是,当我执行ds时,我得到了以下消息
ModuleNotFoundError: No module named 'settings'
如果我评论所有的设置,那么我会得到这个错误:
ModuleNotFoundError: No module named 'package_a'
所以python找不到包。
我该如何解决这个问题?
您在__main__.py
中的包名为ds
。
from ds.package_a import name_a
from ds.package_a.package_a_b import name_a_b
from ds.settings import config