简体   繁体   English

Python 3.7.3 Setup.py Package 中的 ModuleNotFoundError

[英]ModuleNotFoundError in Python 3.7.3 Setup.py Package

I have a Python package in the format:我有一个 Python package 格式:

mypackage
├── README.md
├── doc
│   └── README.md
├── examples
│   └── README.md
├── setup.py
└── src
    ├── __init__.py
    ├── core
    │   ├── __init__.py
    │   └── main.py
    └── test
        └── README.md

It has an accompanying setup.py file:它有一个随附的 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)

When I run python setup.py develop or pip install -e.当我运行python setup.py developpip install -e. , the package says it installs successfully. , package 说安装成功。 However, when I run my_package , it gives the error:但是,当我运行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'

I have the __init__.py files in the appropriate places.我在适当的地方有__init__.py文件。 I've tried using setuptools ' explicit find_packages routine.我试过使用setuptools的明确find_packages例程。 What am I doing wrong here?我在这里做错了什么?

Here are two things you can check.您可以检查以下两件事。 Often times, after doing check 1 your console script starts working.通常,在检查 1 之后,您的控制台脚本开始工作。 If not, try check 2 and see if it pinpoints you the problem.如果没有,请尝试检查 2 并查看它是否能指出您的问题。

Check 1: Correct python executable & pip combination检查 1:正确的 python 可执行文件和 pip 组合

Sometimes the plain pip install installs using wrong python executable.有时,普通的pip install会使用错误python可执行文件进行安装。 Try to force the install with the same python executable as you are using when running the script.尝试使用与运行脚本时相同的python可执行文件强制安装。

python -m pip install -e .

To be even more sure, you can check your python executable path and use it directly in place of python :为了更加确定,您可以检查您的 python 可执行路径并直接使用它来代替python

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

Check 2: The code runs without the console script检查 2:代码在没有控制台脚本的情况下运行

Then, check that you can run the script directly.然后,检查您是否可以直接运行该脚本。 If not in the src/core/main.py already, add如果不在src/core/main.py中,添加

if __name__ == '__main__':
    main()

and then, try to run the script directly with然后,尝试直接运行脚本

python src/core/main.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM