virtualenv困惑

所以我打开一个终端,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

然后做:

>>> import foo_package

哪些输出:

importing from normal env

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

编辑:乔恩H告诉我,我需要激活环境。 但这似乎不能解决问题...

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问我什么sys.path在我的virtualenv ...

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-packages/setuptools-0.6c9-py2.6.egg','/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/virtu alenvwrapper-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 “/usr/lib/python2.6”,“ 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','/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']

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

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。 我试过了。 同样的问题。

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中的一个bug。 它尚未更新以正确处理.local目录。 我在bug追踪器上为此提出了一个问题。

更新 :这个bug现在已经在virtualenv 1.4.2及更高版本中修复了。


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

source bin/activate

在您创建的virtualenv内。 你应该看到像这样的东西:

(test_env)computername:foldername$

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

没有这一步,它仍然会使用你的默认Python安装。


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

看着这一点,看起来你正在做的一切正确,但我想提出一个建议,因为你从来没有听说过它:virtualenvwrapper使得使用virtualenv更快更简单。 尝试一下可能会很有趣,看看你是否仍然遇到同样的问题,也许你会发现你错过了什么。

链接地址: http://www.djcxy.com/p/23337.html

上一篇: virtualenv confusion

下一篇: Accessing protected members in a derived class