无法在virtualenv中安装下载的软件包

时间:2012-04-14 12:29:37

标签: python virtualenv

我已经下载了一个存储库Django class fixtures,我想在我的virtualenv中使用python setup.py install安装它:

(test)/tmp/django-class-fixtures $ which python
/home/kuba/.virtualenvs/test/bin/python
(test)/tmp/django-class-fixtures $ python setup.py install
...
Extracting django_class_fixtures-0.8-py2.7.egg to /home/kuba/.virtualenvs/test/lib/python2.7/site-packages
Adding django-class-fixtures 0.8 to easy-install.pth file

Installed /home/kuba/.virtualenvs/test/lib/python2.7/site-packages/django_class_fixtures-0.8-py2.7.egg
Processing dependencies for django-class-fixtures==0.8
Finished processing dependencies for django-class-fixtures==0.8
(test)/tmp/django-class-fixtures $

但是除了鸡蛋之外没有安装:

(test)/tmp/django-class-fixtures $ cdvirtualenv
(test)/home/kuba/.virtualenvs/test $ cd lib/python2.7/site-packages
(test)/home/kuba/.virtualenvs/test/lib/python2.7/site-packages $ ls
django                               PIL
Django-1.4-py2.7.egg-info            PIL.pth
django_class_fixtures-0.8-py2.7.egg  pip-1.1-py2.7.egg
easy-install.pth                     setuptools-0.6c11-py2.7.egg
IPython                              setuptools.pth
ipython-0.12-py2.7.egg-info
(test)/home/kuba/.virtualenvs/test/lib/python2.7/site-packages $

你有什么想法吗?

编辑: 我想从shell导入class_fixtures例如:

(test)/home/kuba/.virtualenvs/test/lib/python2.7/site-packages $ ipython
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
Type "copyright", "credits" or "license" for more information.

IPython 0.11 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import class_fixtures
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/home/kuba/.virtualenvs/test/lib/python2.7/site-packages/<ipython-input-1-6a049da89661> in <module>()
----> 1 import class_fixtures

ImportError: No module named class_fixtures

In [2]:

1 个答案:

答案 0 :(得分:3)

问题不在于没有安装软件包,而是IPython忽略了virtualenv中的软件包,如文档here所述。我想你会发现用python启动交互式shell会让你输入一切正常。

要让IPython识别网站包,请将此类内容放入名为~/.ipython/profile_default/startup/00-virtualenv.py的文件或类似文件中:

from __future__ import print_function
import site
from os import environ
from os.path import join
from sys import version_info

if 'VIRTUAL_ENV' in environ:
    virtual_env = join(
        environ.get('VIRTUAL_ENV'),
        'lib',
        'python%d.%d' % version_info[:2],
        'site-packages'
    )
    site.addsitedir(virtual_env)
    print('VIRTUAL_ENV ->', virtual_env)
    del virtual_env
    del site, environ, join, version_info