Update: I’ve recently switched from Macports to Homebrew. I describe my new configuration in this post.
Here’s a rundown of how I get multiple versions of Python working on OSX. I’m running Snow Leopard on an older Macbook Pro that canĀ only run in 32-bit mode. If you’ve got a newer 64-bit Mac, your mileage may vary. In the next month or so, work will be providing me with a new iMac. If I run into any issues using this technique on that 64-bit machine, I’ll update this post.
Installing Macports
If you don’t already have it installed, grab the DMG file for Macports. Verify that you have Xcode installed, and optionally, the X11 User package if you plan to install ports that use X-Windows (X11 User is installed by default on Leopard and Snow Leopard).
Installing Python
Now that you’ve got Macports installed, its time to install the versions of Python you want to work with. I’ll be describing the steps you’ll take at the Terminal to get all versions of Python that Macports supports installed, but you could just as easily use a Macports GUI like Porticus to install these packages. You might also only want a subset of these packages installed.
From a shell prompt, enter the following commands.
[text]
sudo port -v selfupdate
sudo port install python24
sudo port install python25
sudo port install python26
sudo port install python27
sudo port install python31
sudo port install python_select
[/text]
At the time his post was written, executing these commands would install Python 2.4.6, 2.5.5, 2.6.6, 2.7, and 3.1.2, along with python_select into /opt/local/bin. Running python_select with the -l option from the Terminal should result in something like this.
[shell]
[dale@DaleBook ~]$ python_select -l
Available versions:
current none python24 python25 python26 python26-apple python27 python31
[/shell]
On Snow Leopard, you’ll see a python26-apple option like above. On Leopard, you’ll see python25-apple. This is the system Python installation.
Installing Virtualenv
If you haven’t done so already, now it is time to install virtualenv and virtualenvwrapper into your system Python. Now, you don’t have to install them into your main Python. You could switch to another one of the installed versions with python_select, but then you’d have to remember to switch to that version before using your virtual environments, and you might encounter configuration issues with editors. I just find installing these into the system Python results in less headaches.
Both of these are installable via Macports, easy_install, or pip. I’ll stick to the easy_install route, although I tend to favor pip when working with the virtual environments themselves.
[shell]
sudo easy_install virtualenv
sudo easy_install virtualenvwrapper
[/shell]
Pick a location to store your virtual environments in (I use ~/Envs), then add an export statement to your ~/.profile file to point WORKON_HOME to this location. See the virtualenvwrapper docs for more information on configuring this if you run into problems.
Create and Use a Virtual Environment
Now lets try it all out. Let’s create a virtual environment running Python 2.7. From the Terminal, enter the following:
[text]
mkvirtualenv –no-site-packages –python=/opt/local/bin/python2.7 py27
[/text]
This should result in a new virtual environment based on Python 2.7. By changing the path specified with the –python option, you’ll be able to create a clean install of any of the versions of Python supported by Macports. Again, see the virtualenvwrapper documentation for more information on how to switch between and manage your virtual environments.
This doesn’t seem to work for me at all.
I have macports, and all my python versions, python_select etc working great in there.
if I do easy_install virutalenv etc.. that will go to /Library/etc.. into the system python.
after I set my WORKON_HOME, and then do source .bashrc I see:
[502][rstudner.rs: /Users/me]$ source .bashrc
Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named virtualenvwrapper.hook_loader
virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=/opt/local/bin/python and that PATH is set properly.
Thus, it is freakin’ out about macports python path etc versus the system path.
and if I then go ahead and just do mkvirtualenv temp
go into there/bin.. the python like is to my system python, not to one of the macport pythons..
Any thoughts?
Hi Roger,
Sorry for taking so long to reply.
After you installed virtualenvwrapper, did you make sure to add code to your bash profile to source the virtualenvwrapper.sh? For example, I’ve got this in my ~/.profile under OSX:
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
source /usr/local/bin/virtualenvwrapper.sh
fi
export WORKON_HOME=$HOME/.virtualenvs
back, after months.. still no luck
I can actually do the above *2.7* fine.
but if I try with 2.6 I get this:
mkvirtualenv –python=/opt/local/bin/python2.6 test
Running virtualenv with interpreter /opt/local/bin/python2.6
New python executable in test/bin/python
Installing setuptools…………..
Complete output from command /Users/rstudner/.virtualenvs/t…ython -c “#!python
\”\”\”Bootstrap setuptoo…
” /Library/Python/2.6/site-packa…6.egg:
Traceback (most recent call last):
File “”, line 278, in
File “”, line 238, in main
File “build/bdist.linux-i686/egg/setuptools/command/easy_install.py”, line 21, in
File “build/bdist.linux-i686/egg/setuptools/package_index.py”, line 2, in
File “/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”, line 111, in
from urllib import (unwrap, unquote, splittype, splithost, quote,
File “/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib.py”, line 1335, in
from _scproxy import _get_proxy_settings, _get_proxies
ImportError: No module named _scproxy
—————————————-
…Installing setuptools…done.
Traceback (most recent call last):
File “/Library/Python/2.6/site-packages/virtualenv-1.5.1-py2.6.egg/virtualenv.py”, line 1647, in
main()
File “/Library/Python/2.6/site-packages/virtualenv-1.5.1-py2.6.egg/virtualenv.py”, line 558, in main
prompt=options.prompt)
File “/Library/Python/2.6/site-packages/virtualenv-1.5.1-py2.6.egg/virtualenv.py”, line 654, in create_environment
install_setuptools(py_executable, unzip=unzip_setuptools)
File “/Library/Python/2.6/site-packages/virtualenv-1.5.1-py2.6.egg/virtualenv.py”, line 384, in install_setuptools
_install_req(py_executable, unzip)
File “/Library/Python/2.6/site-packages/virtualenv-1.5.1-py2.6.egg/virtualenv.py”, line 360, in _install_req
cwd=cwd)
File “/Library/Python/2.6/site-packages/virtualenv-1.5.1-py2.6.egg/virtualenv.py”, line 624, in call_subprocess
% (cmd_desc, proc.returncode))
OSError: Command /Users/rstudner/.virtualenvs/t…ython -c “#!python
\”\”\”Bootstrap setuptoo…
” /Library/Python/2.6/site-packa…6.egg failed with error code 1
Also, are you running Snow Leopard? Unlike 2.7, there’s already a 2.6 on Snow Leopard. Before I switched to the MacPorts configuration I blogged about, I used the Python.org version of 2.6. It ended up using the same site-packages folder as the OS version of Python, so if I installed a package under Python.org Python, I’d see it installed under the OS version, too. I don’t know if MacPorts has the same issue or not. Do you get the same error when you run use –no-site-packages?
Another thing you might try is completely removing MacPorts and reinstalling it fresh. I do this occasionally when a MacPorts installer fubars my installation. I’d then verify virtualenv and virtualenvwrapper work with my system Python on their own. I’d then reinstall MacPorts and the various versions of Python I wanted to work with and see if virtualenv and virtualenvwrapper then worked.
To uninstall MacPorts from the default location /opt/local/, perform this command from a terminal window.
%% sudo rm -rf \
/opt/local \
/etc/manpaths.d/macports \
/etc/paths.d/macports \
/Applications/DarwinPorts \
/Applications/MacPorts \
/Library/LaunchDaemons/org.macports.* \
/Library/Receipts/DarwinPorts*.pkg \
/Library/Receipts/MacPorts*.pkg \
/Library/StartupItems/DarwinPortsStartup \
/Library/Tcl/darwinports1.0 \
/Library/Tcl/macports1.0
Here’s the command all on one line:
sudo rm -rf /opt/local /etc/manpaths.d/macports /etc/paths.d/macports /Applications/DarwinPorts /Applications/MacPorts /Library/LaunchDaemons/org.macports.* /Library/Receipts/DarwinPorts*.pkg Library/Receipts/MacPorts*.pkg /Library/StartupItems/DarwinPortsStartup /Library/Tcl/darwinports1.0 /Library/Tcl/macports1.0
I use Porticus, and I tend to uninstall it and its plist files deleting MacPorts. I reinstall it after I get my basic MacPorts installed, then us it to install packages. Finally, I tend not to install any Packages in my global site-packages folders other than virtualenv and virtualenvwrapper in the system Python, favoring everything in the sandbox instead. I’ll occasionally break this rule if there’s some gnarly CPython build that requires a lot of installed C libraries.
On the version I downloaded just now with Snow Leapord I had to add these lines to my .profile:
if [ -f /opt/local/bin/virtualenvwrapper_bashrc-2.6 ]; then
source /opt/local/bin/virtualenvwrapper_bashrc-2.6
fi
export WORKON_HOME=$HOME/.virtualenvs
It seems that virtualenvwrapper.sh did not exist for me, this is probably because I installed py26-virtualenvwrapper with macports instead of going the easy_install route
The error’s message:
ImportError: No module named virtualenvwrapper.hook_loader
virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=/opt/local/bin/python and that PATH is set properly.
Can come from a the fact than you have several versions of python
installed and the virtualenv in use isn’t the right one…
So after you installed a new python version you should provide
do reinstall a virtualenv for this python version with the “-I” flag on pip install.
So the way to have a new virtuaelenv installed is:
$pip install -I virtualenv
After that you can follow to install virtualenvwrapper.
I was stuck with the same error …
Import Error: No module named virtualenvwrapper.hook_loader
virtualenvwrapper.sh: There was a problem running the initialization hooks. If
Python could not import the module virtualenvwrapper.hook_loader, check
that virtualenv has been installed for
VIRTUALENVWRAPPER_PYTHON=/opt/local/bin/python and that PATH is set
properly.
… because virtualenvwrapper.sh was calling a different version of python from the one that was used when virtualenvwrapper.sh was installed. (I had changed my $PATH)
Instead of installing a new virtualenvwrapper for the new python, I directed virtualenvwrapper to use the old python by adding the following to my .bash_profile
VIRTUALENVWRAPPER_PYTHON=
… apologies. The last line of the above post should read:
VIRTUALENVWRAPPER_PYTHON=XXX
I substituted XXX with the default python from when virtualenvwrapper was installed
I have this “ImportError: No module named virtualenvwrapper.hook_loader” problem too. I’m on OS X 10.5 Leopard, previously I had python 2.6 and I was using virtualenvwrapper then, but when I used Macports to install python 2.7, now I get this problem.
Still trying to figure it out. I want to do what Stu did, but instead of telling virtualenv to use the old python, I want it to use the new python. Any ideas, guys?
Hi Jay, sorry for the late reply. I wish I could help more with your macports/virtualenv issues, but I’ve since moved over to Homebrew for package management on OSX. I recently did a clean install of Snow Leopard, as well. My system site-packages folder was filled with a lot of cruft from before I started using virtualenv, and it seemed like the quickest way to start out fresh. Everything’s rocking along quite smoothly now with Python 2.7.2 and 3.2.1 VMs.