【发布时间】:2020-07-06 10:09:06
【问题描述】:
我的包结构如下:
module_installer/
|-- module_installer
| `-- __init__.py
`-- setup.py
setup.py
from setuptools import setup
setup(name='module_installer')
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 for module-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 是否安装正确且类不可导入?
【问题讨论】:
-
您的
PYTHONPATH是什么,module_installer目录具体在哪里? -
@JohnGordon
PYTHONPATH确实包含/home/user/Envs/se_ena/lib/python3.7/site-packages。module_installer是我的主目录。 -
修改
PYTHONPATH(或sys.path)几乎没有充分的理由。我建议尽量避免做这样的事情,几乎总是有一个更明智的解决方案。
标签: python python-3.x setuptools