Python开发模式:ModuleNotFoundError:没有名为xxxx的模块

时间:2018-02-18 22:30:56

标签: python setuptools packaging setup.py pypi

我在python中有一个项目,如果通过“python do.py”调用它可以正常工作,但是当我在开发模式下安装它并且脚本导入我的模块时,我得到一个“ModuleNotFoundError”。这是我的setup.py

from setuptools import setup, find_packages


with open('README.rst') as f:
    readme = f.read()

with open('LICENSE') as f:
    license = f.read()

setup(
    name='do-pack',
    version='0.1dev',
    description='A command-line tool to create python packages',
    long_description=readme,
    author='Carlos Montecinos Geisse',
    author_email='carlos.w.montecinos@gmail.com',
    url='https://github.com/wilfredinni/do-pack',
    license=license,
    packages=find_packages(exclude=('tests', 'docs')),
    include_package_data=True,
    install_requires=['click'],
    python_requires='>=3',
    entry_points={
        'console_scripts': [
            'do = do.do:main',
        ]
    },
)

这是结构(主要和模块在do /中):

[树] [1]

这是我使用python do.py时的调用:

(python-cli) λ python do.py
Usage: do.py [OPTIONS] COMMAND [ARGS]...

  Simple CLI based script to make your Repository Structure.

Options:
  --help  Show this message and exit.

Commands:
  assistant  A step by step assistant.
  create     creates an empty proyect structure.

并且,这是我在开发模式下运行时的输出:

(python-cli) λ do
Traceback (most recent call last):
  File "C:\Users\carlo\Envs\python-cli\Scripts\do-script.py", line 11, in <module>
    load_entry_point('do-pack', 'console_scripts', 'do')()
  File "c:\users\carlo\envs\python-cli\lib\site-packages\pkg_resources\__init__.py", line 572, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "c:\users\carlo\envs\python-cli\lib\site-packages\pkg_resources\__init__.py", line 2755, in load_entry_point
    return ep.load()
  File "c:\users\carlo\envs\python-cli\lib\site-packages\pkg_resources\__init__.py", line 2408, in load
    return self.resolve()
  File "c:\users\carlo\envs\python-cli\lib\site-packages\pkg_resources\__init__.py", line 2414, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "c:\dropbox\dropbox\projects\python\do-pack\do\do.py", line 3, in <module>
    import skeleton
ModuleNotFoundError: No module named 'skeleton'

谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 确保do文件夹中包含__init__.py文件。 <击> 2。将“do”添加到setup.py 中的packages= kwarg 看起来您正在使用find_packages()
  2. 处理这个问题

    工作示例

    + app
    |+ do
     |+ __init__.py
     |+ do.py
    

    setup.py

    来自setuptools导入设置

    setup(
        name="cliapp",
        version='0.1',
        packages=['do'],
        entry_points='''
            [console_scripts]
            do=do.do:run
        ''',
    )
    

    do.py

    def run():
        print('Completed')
    

    从app /文件夹中,安装并运行:

    >>> pip install .
    >>> do
    Completed
    

    更新

    我在本地运行你的代码,看起来它可能只是代码中的python相对/绝对导入问题。

    如果您使用完全绝对导入,它应该起作用:

    do.py

    而不是

    import skeleton
    import licenses
    import setup_config
    

    您需要使用绝对或相对包导入

    # Absolute
    from do import skeleton
    from do import licenses
    from do import setup_config
    
    # Relative
    from . import skeleton
    from . import licenses
    from . import setup_config