简体   繁体   English

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

[英]Python Development Mode: ModuleNotFoundError: No module named xxxx

i have a project in python that works just fine if call it by "python do.py", but when i install it in Development Mode and the script imports my modules i get a "ModuleNotFoundError". 我在python中有一个项目,如果通过“ python do.py”调用它就可以正常工作,但是当我在“开发模式”下安装该项目并且脚本导入了我的模块时,我得到了“ ModuleNotFoundError”。 This is my setup.py 这是我的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',
        ]
    },
)

This is the structure (main and modules are in do/): 这是结构(main和模块位于do /中):

[tree][1] [树] [1]

This is the call when i use python do.py: 这是我使用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.

and, this is the output when i run it in development mode: 而且,这是我在开发模式下运行时的输出:

(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'

Thank you. 谢谢。

  1. Make sure the do folder has an __init__.py file inside. 确保do文件夹中包含__init__.py文件。 2. Add 'do' to the packages= kwarg in your setup.py Looks like you are handling that ok with find_packages() 2.在您的setup.py中将'do'添加到 packages= kwarg中。 看起来您正在使用find_packages()处理该确定

Working Example 工作实例

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

setup.py setup.py

from setuptools import setup 从setuptools导入设置

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

do.py py

def run():
    print('Completed')

From app/ folder, install and run: 在app /文件夹中,安装并运行:

>>> pip install .
>>> do
Completed

Update 更新资料


I ran your code locally and looks like it might just be a python relative/absolute import issues inside your code. 我在本地运行您的代码,看起来它可能只是代码中的python相对/绝对导入问题。

If you use full absolute imports it should work: 如果您使用完全绝对导入,则应该可以使用:

do.py py

Instead of 代替

import skeleton
import licenses
import setup_config

You need to use absolute or relative package imports 您需要使用绝对或相对软件包导入

# 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

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

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