简体   繁体   English

已安装 Python 包,但 __init__ 中的内容不可导入

[英]Python package installed but content from __init__ isn't importable

I have the following package structure:我有以下包结构:

module_installer/   
|-- module_installer
|   `-- __init__.py 
`-- setup.py        

setup.py设置文件

from setuptools import setup                             
setup(name='module_installer')

module_installer/__init__.py模块安装程序/__init__.py

class ImportMe():
    pass         

Being in the "root directory" of the package the class ImportMe is importable:在包的“根目录”中,类ImportMe是可导入的:

module_installer$ tree --charset=ASCI
|-- module_installer
|   `-- __init__.py
`-- setup.py
python -c "from module_installer import ImportMe"
# This makes sense. The current dir is in python path and the `module_installer` has `__init__.py.

However if I install it and try to run it from a different directory it fails:但是,如果我安装它并尝试从其他目录运行它,则会失败:

module_installer$ pip install .
module_installer$ cd /some_other_dir
some_other_dir$ python -c "from module_installer import ImportMe"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'ImportMe' from 'module_installer' (unknown location)

Grepping pip freeze for module-installer shows the packages successfully installed. Grepping pip freeze for module-installer显示软件包已成功安装。

Exploring the package for files doesn't show the package installed:浏览文件包不会显示已安装的包:

$ pip show -f module-installer
...
Location: /home/user/Envs/se_ena/lib/python3.7/site-packages
...
Files:
  module_installer-0.0.0.dist-info/INSTALLER
  module_installer-0.0.0.dist-info/METADATA
  module_installer-0.0.0.dist-info/RECORD
  module_installer-0.0.0.dist-info/WHEEL
  module_installer-0.0.0.dist-info/top_level.txt
# No traces of module_installer/__init__.py?

Doesn't the __init__.py get installed correctly and the class isn't importable? __init__.py没有正确安装并且类不可导入吗?

Looks to me like the setuptools.setup function call in setup.py is missing the list of packages as an argument to the packages parameter.在我看来, setup.pysetuptools.setup函数调用缺少包列表作为packages参数的参数。

setup.py : setup.py

setup(
    # ...
    packages=['module_installer'],
    # ...
)

To avoid listing packages manually, setuptools provides the following utility functions:为了避免手动列出软件包, setuptools提供了以下实用功能:

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

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