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

时间:2020-03-25 19:27:02

标签: python python-3.x setuptools

我的包装结构如下:

Caused: hudson.remoting.ProxyException: org.codehaus.groovy.runtime.InvokerInvocationException: org.codehaus.groovy.runtime.InvokerInvocationException: org.codehaus.groovy.runtime.InvokerInvocationException: hudson.AbortException: script returned exit code 100
    at doSomething2.call(doSomething2.groovy:24)
    at doSomething3.run(doSomething3.groovy:27)
    at doSomething1.call(doSomething1.groovy:41)
    at WorkflowScript.run(WorkflowScript:1) //This is the Jenkins File I believe

setup.py

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

module_installer / __ init __。py

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

在包的“根目录”中,类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) 替换为pip freeze将显示已成功安装软件包。

浏览软件包的文件不会显示已安装的软件包:

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? 是否正确安装并且该类不可导入?

1 个答案:

答案 0 :(得分:2)

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

setup.py

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

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