2

我有一个 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 developorpip 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例程。我在这里做错了什么?

4

1 回答 1

0

您可以检查以下两件事。通常,在检查 1 之后,您的控制台脚本开始工作。如果没有,请尝试检查 2 并查看它是否能指出您的问题。

检查 1:正确的 python 可执行文件和 pip 组合

有时普通pip install安装使用错误的python可执行文件。尝试使用与python运行脚本时使用的相同的可执行文件强制安装。

python -m pip install -e .

为了更加确定,您可以检查您的 python 可执行路径并直接使用它来代替python

python -c "import sys; print(sys.executable)"

检查 2:代码在没有控制台脚本的情况下运行

然后,检查您是否可以直接运行该脚本。如果还没有src/core/main.py,请添加

if __name__ == '__main__':
    main()

然后,尝试直接运行脚本

python src/core/main.py
于 2020-07-20T18:04:59.287 回答