python - 已安装 Python 包,但来自 __init__ 的内容不可导入
问题描述
我有以下包结构:
module_installer/
|-- module_installer
| `-- __init__.py
`-- setup.py
安装程序.py
from setuptools import setup
setup(name='module_installer')
模块安装程序/__init__.py
class ImportMe():
pass
在包的“根目录”中,该类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.
但是,如果我安装它并尝试从不同的目录运行它,它会失败:
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
formodule-installer
显示软件包安装成功。
探索文件包不显示已安装的包:
$ 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?
没有__init__.py
正确安装并且类不可导入吗?
解决方案
在我看来,setuptools.setup
函数调用setup.py
缺少包列表作为参数的packages
参数。
setup.py
:
setup(
# ...
packages=['module_installer'],
# ...
)
为避免手动列出软件包,setuptools提供了以下实用功能:
推荐阅读
- python - Pytest 的“caplog”夹具的类型提示是什么?
- regex - 如何删除前导字符直到满足 1-9
- javascript - 为什么javascript indexOf方法给我错误的输出
- excel - 在 excel 中,如何根据另一张表的条件自动填充一张表?
- python - pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime:越界纳秒时间戳:9999-12-31 00:00:00
- nestjs - 将信息从 NestJS 中间件传递到控制器/服务
- android - recyclerview添加元素时不添加元素的问题
- vpn - 作为 vpn 连接到远程桌面
- python - 在 Python 中过滤嵌套字典
- javascript - 如何在本机基础的组件框之间创建空白