Python 3.x 使用pip打包后出现ModuleNotFound错误

Python 3.x 使用pip打包后出现ModuleNotFound错误,python-3.x,pip,Python 3.x,Pip,为什么在运行pip3 install.后执行命令提示符脚本时,我会收到一个ModuleNotFoundError:没有名为“helpers”的模块。?helpers的位置是c:\users\username\appdata\local\programs\python\python36-32\lib\site packages\mypackage\helpers.py那么如何让程序在那里查看呢 PS C:\> mypackage Traceback (most recent call las

为什么在运行
pip3 install.
后执行命令提示符脚本时,我会收到一个
ModuleNotFoundError:没有名为“helpers”的模块。
helpers
的位置是
c:\users\username\appdata\local\programs\python\python36-32\lib\site packages\mypackage\helpers.py
那么如何让程序在那里查看呢

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'
以下是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/\uuu init\uuuu.py时,该程序确实正确运行。

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

from mypackage import helpers as h
要执行相对导入,请显式执行:

from . import helpers as h

谢谢你,博士。那帮我修好了。对于任何使用此答案的人,这里是支持此答案的。虽然它是针对Python2.5的,但在本例中,这些原则非常适用。
from . import helpers as h