已安装Python包,但来自_init__的内容不可导入

2024-09-28 22:32:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下包结构:

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

setup.py

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

模块安装程序/\uuuuu初始化.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)

module-installerpip freeze进行灰显显示已成功安装的软件包

在包中查找文件时,不会显示已安装的包:

$ 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安装是否正确,类是否不可导入


Tags: pipnameinfrompyimportinfoinit
1条回答
网友
1楼 · 发布于 2024-09-28 22:32:45

在我看来setup.py中的setuptools.setup函数调用缺少作为packages参数参数的包列表

setup.py

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

为避免手动列出软件包,setuptools提供以下实用程序功能:

相关问题 更多 >