flinnb
flinnb

Reputation: 53

Trouble creating a virtualenv for python3 on OS X

I'm having a strange problem with a virtualenv I've created for python 3. I went through the usual steps:

$ virtualenv --python=/opt/local/bin/python3.3 .py3
$ source .py3/bin/activate

The problem I'm having is that when I call python --version, it is still reporting 2.7.5, even though the paths all appear to be set up correctly. My virtualenv was created in /Users/barry.flinn/projects/.py3/bin, and I get thes results when I run which python:

$ which python
/Users/barry.flinn/projects/.py3/bin/python

The bin folder has the following python executables:

lrwxr-xr-x   1 barry.flinn  obfuscated\Domain Users     9 Sep 23 19:39 python -> python3.3
lrwxr-xr-x   1 barry.flinn  obfuscated\Domain Users     9 Sep 23 19:39 python3 -> python3.3
-rwxr-xr-x   1 barry.flinn  obfuscated\Domain Users  9100 Sep 23 19:39 python3.3

Clearly, invoking python should give me python 3.3, and yet it still seems to revert to the system python, which is 2.7.5. My $PATH when the virtualenv is active is:

 /Users/barry.flinn/projects/.py3/bin:/usr/local/mysql/bin:/opt/local/lib/postgresql92/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin:/usr/local/munki

Since all of this seems correct, I'm stumped as to what is going on here.

Update:

This reports correctly:

$ env python --version
Python 3.3.2

Which, to me, is slightly more baffling.

Upvotes: 4

Views: 892

Answers (1)

bogatron
bogatron

Reputation: 19179

If you have a shell alias defined for the python interpreter, it will override the python chosen for your virtualenv. For example:

user@x790:~/temp$ alias python=/usr/bin/python
user@x790:~/temp$ python --version
Python 2.7.4
user@x790:~/temp$ virtualenv --python=/usr/bin/python3 foo
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in foo/bin/python3
Also creating executable in foo/bin/python
Installing distribute.................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................done.
Installing pip................done.
user@x790:~/temp$ . foo/bin/activate
(foo)user@x790:~/temp$ python --version
Python 2.7.4

Note that the python version reported was 2.7.4, even though python3 was selected for the virtualenv. Using the env command circumvents the alias:

(foo)user@x790:~/temp$ env python --version
Python 3.3.1

Lastly, you can get around this issue by either temporarily disabling the python alias for a single command or permanently undefining it:

(foo)user@x790:~/temp$ \python --version
Python 3.3.1
(foo)user@x790:~/temp$ unalias python
(foo)user@x790:~/temp$ python --version
Python 3.3.1

Upvotes: 2

Related Questions