我有一个 Python 包,格式如下:
mypackage
├── README.md
├── doc
│ └── README.md
├── examples
│ └── README.md
├── setup.py
└── src
├── __init__.py
├── core
│ ├── __init__.py
│ └── main.py
└── test
└── README.md
它有一个随附的 setup.py 文件:
import os
from setuptools import setup, find_packages
def read(fname):
"""
Reads the README functions are prints them into the long_description in
the setup routine.
Parameters
----------
fname : README file name
Returns
-------
Rendered README
"""
return open(os.path.join(os.path.dirname(__file__), fname)).read()
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Programming Language :: Python",
]
def run_setup():
"""
This functions holds the setup command. Rather than running setup directly,
it is wrapped in a 'try-except' that will print out errors if they occur.
"""
setup(
name='My Package',
version='0.1',
description='My package',
long_description=read('README.md'),
long_description_content_type='text/markdown',
classifiers=classifiers,
packages=['src', 'src.core'],
python_requires='>=2.7.9',
entry_points="""
[console_scripts]
my_package = src.core.main:main
""",
)
try:
run_setup()
except SystemExit as e:
print(e)
当我运行python setup.py develop
orpip install -e .
时,包说它安装成功。但是,当我运行时my_package
,它会给出错误:
Traceback (most recent call last):
File "/Users/mm/opt/anaconda3/bin/my_package", line 33, in <module>
sys.exit(load_entry_point('My-Package', 'console_scripts', 'my_package')())
File "/Users/mm/opt/anaconda3/bin/my_package", line 25, in importlib_load_entry_point
return next(matches).load()
File "/Users/mm/opt/anaconda3/lib/python3.7/site-packages/importlib_metadata/__init__.py", line 105, in load
module = import_module(match.group('module'))
File "/Users/mm/opt/anaconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'src.core'
我__init__.py
在适当的地方有文件。我试过使用setuptools
' 显式find_packages
例程。我在这里做错了什么?