views:

50

answers:

2

I created a virtualenv environment with the --no-site-packages option. After activating the virtualenv, I noticed that importing psycopg2 at the "python" prompt would import the out of date system library I have but importing it at the "python2.6" prompt would import the newer version of the library I installed into the virtualenv.

Why is this? How can I only work with the virtualenv packages when I have a virtualenv activated?

I am on OS X, if it matters.

Edit in response to Jeff's comments below:

There are both "python" and "python2.6" executables in my virtualenv /bin directory. "python2.6" is a symbolic link to "python" and "python" is a binary.

(ice_development)[jacob@Beagle:~] $ ls -l Virtualenv/ice_development/bin/
total 264
-rw-r--r--  1 jacob  staff   2086 Sep  8 18:13 activate

 .....

-rwxr-xr-x  1 jacob  staff  50720 Sep  8 18:13 python
lrwxr-xr-x  1 jacob  staff      6 Sep  8 18:13 python2.6 -> python

With the ENV activated, "which python" and "which python2.6" both point to the ENV directory.

(ice_development)[jacob@Beagle:~] $ which python
/Users/jacob/Virtualenv/ice_development/bin/python
(ice_development)[jacob@Beagle:~] $ which python2.6
/Users/jacob/Virtualenv/ice_development/bin/python2.6
(ice_development)[jacob@Beagle:~] $ 

Moreover, the prompt is identical after using the executables at the command line.

(ice_development)[jacob@Beagle:~] $ python2.6
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.2.2 (dt dec ext pq3)'
>>> quit()

(ice_development)[jacob@Beagle:~] $ python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.0.13 (dt dec ext pq3)'
>>> quit()

The ~/ENV/lib/python2.6/site-packages directory contains the NEW version of psycopg2 (2.2.2):

(ice_development)[jacob@Beagle:~] $ ls Virtualenv/ice_development/lib/python2.6/site-   packages/
Twisted-10.1.0-py2.6-macosx-10.6-universal.egg       setuptools-0.6c11-py2.6.egg
easy-install.pth                                     setuptools.pth
pip-0.7.2-py2.6.egg                                  txpostgres-0.3.0-py2.6.egg
psycopg2                                             zope.interface-3.6.1-py2.6-macosx-    10.6-universal.egg
psycopg2-2.2.2-py2.6.egg-info

However, importing psycopg2 at the different prompts imports two different versions.

+1  A: 

I've been trying to replicate your problem but with no luck.

Activating virtualenv leaves me with a prompt like this:

jeff@DeepThought:~$ source ~/ENV/bin/activate
(ENV)jeff@DeepThought:~$ 

Mostly what this is doing is adding the ~/ENV/bin to the front of the search path so when I type "python" the version I have installed in that bin comes up first. In my case, I have 2.6 installed globally and 2.7 installed virtually.

(ENV)jeff@DeepThought:~$ python
Python 2.7 (r27:82500, Sep  8 2010, 20:09:26) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

What I find strange about your case is that you say you have your updated libraries in the virtual environment, but you are only able to access them with python2.6. Unless you have created it on your own, ~/ENV/bin should not even have a python2.6 executable. If you have activated virtualenv, typing python should bring you to the virtualenv python shell and typing python2.6 would bring you to the global python shell. If that were the case, you should be seeing the opposite of what you say is happening.

The first thing I would do is check out what is being executed when you run python and python2.6:

(ENV)jeff@DeepThought:~$ which python
/home/jeff/ENV/bin/python
(ENV)jeff@DeepThought:~$ which python2.6
/usr/bin/python2.6

This looks how I would expect it to. What does yours look like? If yours also looks like that, maybe you need to just go into ~/ENV/lib/python2.6/site-packages/ and remove the files that are giving you trouble, replacing them with the updated files.

EDIT: alias takes priority over search path:

jeff@DeepThought:~$ echo $PATH
/home/jeff/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
jeff@DeepThought:~$ cat > /home/jeff/bin/hello.sh
#!/bin/bash
echo "hello world"
jeff@DeepThought:~$ chmod +x ~/bin/hello.sh 
jeff@DeepThought:~$ hello.sh
hello world
jeff@DeepThought:~$ which hello.sh
/home/jeff/bin/hello.sh
jeff@DeepThought:~$ alias hello.sh=/usr/bin/python
jeff@DeepThought:~$ which hello.sh
/home/jeff/bin/hello.sh
jeff@DeepThought:~$ hello.sh
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
xnine
Also check the module search path, `sys.path`, for each: `python -c 'import sys;print(sys.path)'` and `python2.6 -c ...`
Ned Deily
the "python" executable has a much longer search path including the third party system packages in /Library/Python/2.6/site-packages that I do not want to import.
Jacob Lyles
+1  A: 

Thanks to xnine's response, I got the idea to check my .bashrc file. I commented out these lines:

export PATH=/usr/bin/python2.6:$PATH
alias python="/usr/bin/python2.6"
alias pdb='python -m pdb'

and one of them did the trick.

Jacob Lyles
It is definitely the alias. It takes priority over the search path. See example in the edited part of my post.
xnine