Update: As of virtualenv 1.6, Python 3 is supported out of the box, so all this is no longer necessary. I touch on it briefly in this post.
In my last post, I described how to get all the various versions of Python currently supported by Macports working together on OSX with virtualenv and virtualenvwrapper. There was one small problem, though. If you tried to create a new virtual environment with Python 3, you’d encounter this error:
[text]
$ mkvirtualenv –no-site-packages –python=/opt/local/bin/python3.1 bar
Running virtualenv with interpreter /opt/local/bin/python3.1
File "/Library/Python/2.6/site-packages/virtualenv-1.5.1-py2.6.egg/virtualenv.py", line 17
except ImportError, e:
^
SyntaxError: invalid syntax
[/text]
This occurs because the except syntax has changed in Python 3, and setuptools, pip, and virtualenv haven’t been updated as yet. However, we can still get Python 3 to play nice with virtualenvwrapper. With Brandon Rhode’s virtualenv3 fork and a bit of bash profile arcanum, we can get Python 3 to play nice with virtualenvwrapper and the other versions of Python installed on our system.
First, go grab virtualenv3 from bitbucket and install it using Python 3. The example below assumes you’ve installed Python 3.1 and python_select via macports as described in my last post, and that the default Snow Leopard Python 2.6.1 is active.
[text]
sudo python_select python31
hg clone http://bitbucket.org/brandon/virtualenv3
cd virtualenv
python setup.py develop
sudo python_select python26-apple
[/text]
Now open your bash ~/.profile in an editor and add the following aliases and functions:
[text]
alias mkve24=’mkvirtualenv –no-site-packages –python=/opt/local/bin/python2.4′
alias mkve25=’mkvirtualenv –no-site-packages –python=/opt/local/bin/python2.5′
alias mkve26=’mkvirtualenv –no-site-packages –python=/opt/local/bin/python2.6′
alias mkve27=’mkvirtualenv –no-site-packages –python=/opt/local/bin/python2.7′
function mkve31() {
/opt/local/bin/python3.1 /Users/daledavis/Source/virtualenv3/virtualenv3.py –no-site-packages $WORKON_HOME/$@
}
[/text]
This assumes you have virtualenvwrapper installed with the WORKON_HOME environment variable. Also note that the mkve31 function body is all on one line despite what my skinny WordPress theme might suggest.
Now we should be able to create virtual environments with all the installed Python versions without hang up. Open up a new terminal and try creating a Python 2.7 and Python 3.1 virtual environment. If all is as it should be, you should see something very similar to what I’ve pasted below.
[text]
$ mkve27 py27
Running virtualenv with interpreter /opt/local/bin/python2.7
New python executable in py27/bin/python
Installing setuptools……………………….done.
virtualenvwrapper.user_scripts Creating /Users/daledavis/VirtualEnvs/py27/bin/predeactivate
virtualenvwrapper.user_scripts Creating /Users/daledavis/VirtualEnvs/py27/bin/postdeactivate
virtualenvwrapper.user_scripts Creating /Users/daledavis/VirtualEnvs/py27/bin/preactivate
virtualenvwrapper.user_scripts Creating /Users/daledavis/VirtualEnvs/py27/bin/postactivate
virtualenvwrapper.user_scripts Creating /Users/daledavis/VirtualEnvs/py27/bin/get_env_details
(py27)$ mkve31 py31
New python executable in /Users/daledavis/VirtualEnvs/py31/bin/python
Installing setuptools…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………done.
(py27)$ python –version
Python 2.7
(py27)$ workon py31
(py31)$ python –version
Python 3.1.2
[/text]
Leave a Reply