用pip打包后出现ModuleNotFoundError [英] ModuleNotFoundError after packaging with pip

查看:198
本文介绍了用pip打包后出现ModuleNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么从包的根目录中运行pip3 install .后执行命令提示符脚本时会收到ModuleNotFoundError: No module named 'helpers'? helpers的位置是c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\mypackage\helpers.py,所以如何让程序在那看?

Why would I receive a ModuleNotFoundError: No module named 'helpers' when I execute the command prompt script after running pip3 install . from my package's root directory? The location of helpers is c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\mypackage\helpers.py so how to I get the program to look there?

PS C:\> mypackage
Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\Scripts\mypackage-script.py", line 11, in <module>
    load_entry_point('mypackage==0.1.2', 'console_scripts', 'mypackage')()
  File "c:\users\username\appdata\local\programs\python\python36-32\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\username\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2755, in load_entry_point
    return ep.load()
  File "c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2408, in load
    return self.resolve()
  File "c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2414, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\mypackage\__init__.py", line 16, in <module>
    import helpers as h
ModuleNotFoundError: No module named 'helpers'

这是源代码树:

PS C:\Users\username\source\repos\mypackage> tree /F
Folder PATH listing for volume mydisk
Volume serial number is 01234-A321
C:.
│   .gitignore
│   COPYING
│   MANIFEST.in
│   README.rst
│   setup.py
│
├───mypackage
│   │   helpers.py
│   │   __init__.py
│   │
│   └───__pycache__
│           helpers.cpython-36.pyc
│           __init__.cpython-36.pyc
│
└───tests
        itsatrap.py
        __init__.py

这是setup.py:

Here is the setup.py:

PS C:\Users\username\source\repos\mypackage> cat setup.py
from setuptools import setup, find_packages
from codecs import open
from os import path

path_to_here = path.abspath(path.dirname(__file__))

with open(path.join(path_to_here, 'README.rst'), encoding='utf-8') as readme_file:
  readme = readme_file.read()

with open(path.join(path_to_here, 'COPYING'), encoding='utf-8') as license_file:
  license = license_file.read()


setup(
  name = 'mypackage',
  version = '0.1.2',
  license=license,
  description = 'Who knows',
  long_description = readme,
  url = 'https://github.com/user/mypackage',
  keywords = ['help!'],
  packages = find_packages(exclude=['contrib', 'docs', 'tests']),
  install_requires = ['matplotlib','numpy'],
  extras_require = {
      'dev': [''],
      'test': [''],
  },
  entry_points = {
      'console_scripts': [
          'mypackage=mypackage:main'
      ],
  },
)

感谢您的帮助.用Python打包对我来说是新领域!从程序包的根目录执行python mypackage/__init__.py时,该程序确实运行正确.

Any help is appreciated. Packaging in Python is new territory for me! This program did run correctly when doing python mypackage/__init__.py from the package's root directory.

推荐答案

在Python 3中,所有导入都是绝对的.使用完整路径从包中导入模块:

In Python 3 all imports are absolute. Use full path to import a module from a package:

from mypackage import helpers as h

要执行相对导入,请明确地做到这一点:

To perform relative import do it explicitly:

from . import helpers as h

这篇关于用pip打包后出现ModuleNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆