5

所以我打开一个终端,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 的全部意义所在。我在这里错过了什么吗?

编辑: Jon 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是什么......

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/virtualenvwrapper-1.20-py2.6.egg', '/home/jesse/Desktop/test_env/lib/python2.6', '/home/jesse/桌面/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-旧的', '/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', '/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']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', '/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']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', '/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']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' ]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 选项创建环境。我试过了。同样的问题。

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”中的站点包更早。

4

4 回答 4

7

您在 virtualenv 中遇到了一个错误。它尚未更新以正确处理 .local 目录。我已经在 bug tracker 提交了一个问题

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

于 2009-10-27T13:29:09.247 回答
2

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

source bin/activate

.. 在您创建的 virtualenv 中。您应该会看到如下内容:

(test_env)computername:foldername$

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

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

于 2009-10-26T11:32:41.637 回答
0

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

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

于 2009-10-26T11:30:56.243 回答
0

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

于 2009-10-26T22:37:29.200 回答