关于python:virtualenv混乱

virtualenv confusion

所以我打开一个终端,cd到我的桌面,然后运行:

virtualenv test_env

然后,我在正常环境中创建以下文件:

/home/jesse/.local/lib/python2.6/site-packages/foo_package/__init__.py

该文件包含一行:

print"importing from normal env"

在test_env中我创建:

/home/jesse/Desktop/test_env/lib/python2.6/site-packages/foo_package/__init__.py

含:

print"importing from test env"

现在我打开一个终端并运行:

$ /home/jesse/Desktop/test_env/bin/python

然后做:

<5233>

哪个输出:

importing from normal env

为什么不从test_env导入文件?我认为这是virtualenv的重点。我在这里错过了什么吗?

编辑:Jon H告诉我,我需要激活环境。但这似乎没有解决问题......

1
2
3
4
5
6
7
8
jesse@jesse-laptop:~/Desktop/test_env$ source bin/activate
(test_env)jesse@jesse-laptop:~/Desktop/test_env$ bin/python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type"help","copyright","credits" or"license" for more information.
>>> import foo_package
importing from normal env
>>>

在相关的情况下使用Ubuntu 9.04 / Python 2.6.2 / virtualenv 1.33。

编辑2:Haes问我在virtualenv中的sys.path是什么...

1
2
3
4
5
6
7
jesse@jesse-laptop:~/Desktop/test_env$ source bin/activate
(test_env)jesse@jesse-laptop:~/Desktop/test_env$ bin/python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type"help","copyright","credits" or"license" for more information.
>>> import sys
>>> sys.path

输出:
['','/ home / jesse /Desktop / test_env / lib / python2.6 / site-package / setuptools-0.6c9-py2.6.egg','/ usr / local / lib / python2.6 / record- packages / enum-0.4.3-py2.6.egg','/ usr / local / lib / python2.6 / decode-packages / cloneginger-1.0.9_beta-py2.6.egg','/ usr / local / lib / python2.6 / dist-packages / ETS-3.2.0-py2.6.egg','/ usr / local / lib / python2.6 / dad-package / TraitsGUI-3.0.4-py2.6.egg ','/ usr / local / lib / python2.6 / dad-packages / TraitsBackendWX-3.1.0-py2.6.egg','/ usr / local / lib / python2.6 / dad-package / TraitsBackendQt-3.1 .0-py2.6.egg','/ usr / local / lib / python2.6 / did-package /yolk-0.41-py2.6.egg','/ usr / local / lib / python2.6 /dist-packages/pylint-0.18.0-py2.6.egg','/usr/local/lib/python2.6/dist-packages/logilab_astng-0.19.0-py2.6.egg','/ usr /local/lib/python2.6/dist-packages/logilab_common-0.39.0-py2.6.egg','/usr/local/lib/python2.6/dist-packages/pudb-0.92.7-py2。 6.egg','/ usr / local / lib / python2.6 / dad-package /Pygments-1.0-py2.6.egg','/ usr / local / lib / python2.6 / dad-packages / ETSProjectTools- 0.5.1-py2.6.egg','/ usr / local / l ib / python2.6 / dist-packages / pydee-0.4.24-py2.6.egg','/ usr / local / lib / python2.6 / dad-packages /visionegg-1.2.1-py2.6-linux -i686.egg','/ usr / local / lib / python2.6 / dad-packages /PyOpenGL-3.0.0c1-py2.6.egg','/ usr / local / lib / python2.6 / dad -packages /Whoosh-0.2.6-py2.6.egg','/usr/local/lib/python2.6/dist-packages/pyinotify-0.8.6-py2.6.egg','/ usr / local / lib /python2.6/dist-packages/svgbatch-0.1.9-py2.6.egg','/usr/local/lib/python2.6/dist-packages/pyglet-1.1.3-py2.6.egg' ,'/ usr / local / lib / python2.6 / dad-packages / lepton-1.0b2-py2.6-linux-i686.egg','/ usr / local / lib / python2.6 / dad-packages / rope -0.9.2-py2.6.egg','/ usr / local / lib / python2.6 / dad-packages /simplejson-2.0.9-py2.6-linux-i686.egg','/ usr / local /lib/python2.6/dist-packages/pymunk-0.8.4-py2.6.egg','/usr/local/lib/python2.6/dist-packages/cssutils-0.9.6-py2.6。 egg','/ usr / local / lib / python2.6 / dist-packages /Shapely-1.0.14-py2.6.egg','/ usr / local / lib / python2.6 / record-packages / sympy- 0.6.5-py2.6.egg','/ usr / local / lib / python2.6 / dist-package / virtualenvwra pper-1.20-py2.6.egg','/ home / jesse /Desktop / test_env / lib / python2.6','/ home / jesse /Desktop / test_env / lib / python2.6 / plat-linux2',' /home/jesse/Desktop/test_env/lib/python2.6/lib-tk','/ home / jesse /Desktop / test_env / lib / python2.6 / lib-old','/ home / jesse / Desktop / test_env /lib/python2.6/lib-dynload','/ usr / lib / python2.6','/ usr / lib / python2.6 / plat-linux2','/ usr / lib / python2.6 / lib- tk','/ home / jesse / .local / lib / python2.6 / site-package','/ home / jesse /Desktop / test_env / lib / python2.6 / site-package','/ usr / local / lib / python2.6 / dist-packages','/ usr / lib / python2.6 / dad-packages','/ usr / lib / python2.6 / record-packages / Numeric','/ usr / lib / python2 .6 / dist-packages / PIL','/ usr / lib / python2.6 / did-package / gst-10.10','/ var / lib / python-support / python2.6','/ usr / lib / python2.6 / dist-packages / gtk-2.0','/ var / lib / python-support / python2.6 / gtk-2.0','/ usr / lib / python2.6 / dad-packages / wx-2.8- GTK2-的unicode']

编辑3:我发现了这个:https://bugs.launchpad.net/ubuntu/+source/python-virtualenv/+bug/339904显然,virtualenv + python 2.6 + ubuntu 9.04存在一些问题。不确定这是否与我的问题有关...我尝试通过Synaptic卸载python-virtualenv包,然后通过easy_install安装版本1.3.4的virtualenv,但仍然有同样的问题...

1
2
3
4
5
6
7
8
9
10
jesse@jesse-laptop:~/Desktop/test_env$ source bin/activate
(test_env)jesse@jesse-laptop:~/Desktop/test_env$ bin/python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type"help","copyright","credits" or"license" for more information.
>>> import foo_package
importing from normal env
>>> import sys
>>> print sys.path
['', '/home/jesse/Desktop/test_env/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg', '/home/jesse/Desktop/test_env/lib/python2.6', '/home/jesse/Desktop/test_env/lib/python2.6/plat-linux2', '/home/jesse/Desktop/test_env/lib/python2.6/lib-tk', '/home/jesse/Desktop/test_env/lib/python2.6/lib-old', '/home/jesse/Desktop/test_env/lib/python2.6/lib-dynload', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/home/jesse/.local/lib/python2.6/site-packages', '/home/jesse/Desktop/test_env/lib/python2.6/site-packages', '/usr/local/lib/python2.6/dist-packages/enum-0.4.3-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/clonedigger-1.0.9_beta-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/ETS-3.2.0-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/TraitsGUI-3.0.4-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/TraitsBackendWX-3.1.0-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/TraitsBackendQt-3.1.0-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/yolk-0.4.1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/pylint-0.18.0-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/logilab_astng-0.19.0-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/logilab_common-0.39.0-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/pudb-0.92.7-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/Pygments-1.0-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/ETSProjectTools-0.5.1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/pydee-0.4.24-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/visionegg-1.2.1-py2.6-linux-i686.egg', '/usr/local/lib/python2.6/dist-packages/PyOpenGL-3.0.0c1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/Whoosh-0.2.6-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/pyinotify-0.8.6-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/svgbatch-0.1.9-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/pyglet-1.1.3-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/lepton-1.0b2-py2.6-linux-i686.egg', '/usr/local/lib/python2.6/dist-packages/rope-0.9.2-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/simplejson-2.0.9-py2.6-linux-i686.egg', '/usr/local/lib/python2.6/dist-packages/pymunk-0.8.4-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/cssutils-0.9.6-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/Shapely-1.0.14-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/sympy-0.6.5-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/virtualenvwrapper-1.20-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/virtualenv-1.3.4-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/enum-0.4.3-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/ETS-3.2.0-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/yolk-0.4.1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/Whoosh-0.2.6-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/pyinotify-0.8.6-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/pyglet-1.1.3-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/simplejson-2.0.9-py2.6-linux-i686.egg', '/usr/local/lib/python2.6/site-packages', '/usr/local/lib/python2.6/site-packages/gtk-2.0', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/Numeric', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/var/lib/python-support/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/var/lib/python-support/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode']

这看起来是向前迈出的一步,因为test_env东西出现在路径中,但它仍然无法正常工作。我认为我目前的问题是'/home/jesse/.local/lib/python2.6/site-packages'出现在'/home/jesse/Desktop/test_env/lib/python2.6/site-packages之前的路径中"

编辑4:Roger建议使用--no-site-packages选项创建env。我试过了。同样的问题。

1
2
3
4
5
6
7
8
9
10
11
jesse@jesse-laptop:~/Desktop/test_env$ source bin/activate
(test_env)jesse@jesse-laptop:~/Desktop/test_env$ bin/python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type"help","copyright","credits" or"license" for more information.
>>> import foo_package
importing from normal env
>>> import sys
>>> sys.path
['', '/home/jesse/Desktop/test_env/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg', '/home/jesse/Desktop/test_env/lib/python2.6', '/home/jesse/Desktop/test_env/lib/python2.6/plat-linux2', '/home/jesse/Desktop/test_env/lib/python2.6/lib-tk', '/home/jesse/Desktop/test_env/lib/python2.6/lib-old', '/home/jesse/Desktop/test_env/lib/python2.6/lib-dynload', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/home/jesse/.local/lib/python2.6/site-packages', '/home/jesse/Desktop/test_env/lib/python2.6/site-packages']
>>>

再次,看起来问题是我的".local"中的网站包在路径中出现的位置早于"test_env"中的网站包。


你遇到了virtualenv中的一个错误。尚未更新以正确处理.local目录。我已经在bug跟踪器上提交了一个问题。

更新:此错误现已在virtualenv 1.4.2及更高版本中修复。


从您提到的步骤来看,您似乎还没有激活虚拟环境。做:

1
source bin/activate

..在你创造的virtualenv中。你应该看到类似的东西:

1
(test_env)computername:foldername$

从这里运行python应该得到你的virtualenv版本。

如果没有此步骤,它仍将使用您的默认Python安装。


您需要使用选项--no-site-packages创建虚拟环境


编辑:我上面的帖子是正确的,你忘了激活。使用virtualenvwrapper我从来没有真正做过那一步所以我的坏:)

看着它,看起来你做的一切都是正确的,但是我想提出你从未听说过的建议:virtualenvwrapper让virtualenv更快更容易。尝试一下可能会很有趣,看看你是否仍然遇到同样的问题,也许你会找到你所缺少的东西。