Python – virtualenv confusion

pythonvirtualenv

So I open a terminal, cd to my desktop, and run:

virtualenv test_env

I then create the following file in my normal environment:

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

This file contains one line:

print "importing from normal env"

In the test_env I create:

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

Containing:

print "importing from test env"

Now I open a terminal and run:

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

And then do:

>>> import foo_package

Which outputs:

importing from normal env

Why doesn't it import the file from test_env? I thought that was the whole point of virtualenv. Am I missing something here?

Edit: Jon H informed me that I need to activate the environment. But this doesn't seem to fix the problem…

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
>>> 

Using Ubuntu 9.04 / Python 2.6.2 / virtualenv 1.33 in case that's relevant.

Edit 2: Haes asked me what sys.path was in my 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

Output:
['', '/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/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', '/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']

Edit 3: I found this: https://bugs.launchpad.net/ubuntu/+source/python-virtualenv/+bug/339904 Apparently there are some issues with virtualenv + python 2.6 + ubuntu 9.04. Not sure if that's related to my issue… I tried uninstalling the python-virtualenv package via Synaptic and then installing version 1.3.4 of virtualenv via easy_install, but still have the same problem…

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']

This looks like a step forward, because test_env stuff is appearing in the path, but it's still not working. I think my current problem is that '/home/jesse/.local/lib/python2.6/site-packages' occurs in the path before '/home/jesse/Desktop/test_env/lib/python2.6/site-packages'

Edit 4: Roger suggested creating the env with the –no-site-packages option. I tried that. Same problem.

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']
>>> 

Again, it looks like the problem is the site-packages in my ".local" appears earlier in the path than the site-packages in "test_env".

Best Answer

You're running into a bug in virtualenv. It has not yet been updated to handle .local directories properly. I've filed an issue for this at the bug tracker.

UPDATE: this bug is now fixed in virtualenv 1.4.2 and later.

Related Topic