Python无法看到使用pip安装的模块

时间:2013-12-18 10:51:43

标签: python virtualenv pip

我正在使用全新的Python virtualenv。我刚刚使用pip安装了httplib2,但Python无法看到它。

(venv)$ sudo pip install httplib2
Requirement already satisfied (use --upgrade to upgrade): httplib2 in     
/usr/local/lib/python2.7/dist-packages
Cleaning up...
(venv)$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import httplib2
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ImportError: No module named httplib2

从我的阅读中,听起来这可能是一个路径问题,但我不知道它为什么会发生,或者如何解决它:(

这是sys.path

的内容
>>> pprint(sys.path)
['',
 '/var/apps/ttex/venv/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg',
 '/var/apps/ttex/venv/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg',
 '/var/apps/ttex/venv/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg',
 '/var/apps/ttex/venv/lib/python2.7/site-packages/pip-1.1-py2.7.egg',
 '/var/apps/ttex/venv/lib/python2.7',
 '/var/apps/ttex/venv/lib/python2.7/plat-linux2',
 '/var/apps/ttex/venv/lib/python2.7/lib-tk',
 '/var/apps/ttex/venv/lib/python2.7/lib-old',
 '/var/apps/ttex/venv/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/var/apps/ttex/venv/local/lib/python2.7/site-packages',
 '/var/apps/ttex/venv/lib/python2.7/site-packages']

奇怪的是,如果我运行Python 2.7,我可以看到包:

(venv)$ python2.7
>>> import httplib2
>>> 

为什么会发生这种情况,我该怎么办呢?

很抱歉,如果这是重复的,那么围绕这个主题有很多问题,但似乎没有一个问题给出简单明确的答案。

1 个答案:

答案 0 :(得分:3)

在新鲜的virtualenv中尝试没有sudo。 Pip认为您希望将其安装在全局范围内,因此要安装在/ usr中,而不是安装在沙盒中。

Python packages not installing in virtualenv using pip