为什么Python`pip.main(['install',...])`给出了ImportError,但仅限于包含点的包名?

时间:2018-01-05 19:53:13

标签: python pip importerror

运行以下代码(Python 2.7.12,在Linux上使用sudo -H)时,从未安装软件包“plaitpy”和“bda.basen”的情况开始,

import sys
import os

# The following code successfully installs bda.basen, then fails to import it.
# However, it works for plaitpy (a random recently updated package).

assert '/usr/local/lib/python2.7/dist-packages' in sys.path

import pip
pip.main(['install', 'plaitpy', 'bda.basen'])

assert '/usr/local/lib/python2.7/dist-packages' in sys.path

assert os.path.isfile('/usr/local/lib/python2.7/dist-packages/plaitpy/__init__.py')
import plaitpy # this succeeds, as expected
print plaitpy 

assert os.path.isfile('/usr/local/lib/python2.7/dist-packages/bda/basen/__init__.py')
import bda.basen # THIS FAILS WITH 'ImportError: No module named bda.basen'
print bda.basen

然后最后一个import语句失败并显示ImportError: No module named bda.basen

根据一些实验,这似乎发生在名称中包含点(例如bda.basenruamel.yaml)的每个包中,并且仅适用于那些。

我的问题:为什么?以及如何解决这个问题?

完整的可运行代码(将更新系统范围的程序包!)位于https://gist.github.com/marnix/2f4efc1154547103bcec3783e6015bfc

1 个答案:

答案 0 :(得分:3)

bdanamespace package,因此不包含bda/__init__.py,因此在您重新启动解释程序之前,无法在Python 2中导入该程序包或手动拨打site.main()。这是因为在.pth中创建了一个site-packages文件,该文件告诉Python这是其他模块所在的命名空间。解释器启动时,所有.pth文件都由site.main()加载,但由于您的文件是在之后创建的,因此Python不了解它。

In [1]: import pip

In [2]: pip.main(['install', 'bda.basen'])
Collecting bda.basen
Requirement already satisfied: setuptools in /usr/local/lib/python2.7/site-packages (from bda.basen)
Installing collected packages: bda.basen
Successfully installed bda.basen-1.1
Out[2]: 0

In [3]: import bda.basen
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-e9d84961fc34> in <module>()
----> 1 import bda.basen

ImportError: No module named bda.basen

In [4]: import site

In [5]: site.main()

In [6]: import bda.basen

In [7]:

Python 3.3+的行为与您期望的一样,因为它本身支持命名空间包,并且不需要您调用site.main()