I've got various versions of python installed on my Mac using Macports. When I've selected python 2.7 via $ port select python python27
, virtualenvwrapper works perfectly.
But if I select another version of python, i.e. 2.6, virtualenvwrapper generates an error message: ImportError: No module named virtualenvwrapper.hook_loader
I checked my .profile and it's setting VIRTUALENVWRAPPER_PYTHON
to /opt/local/bin/python
, so it seems to me virtualenvwrapper should work regardless of which python I've selected.
Any idea what would cause virtualenvwrapper to generate a .hook_loader error when I switch python versions?
You (the OP) seem to have installed virtualenv and virtualenvwrapper with python2.7, and not with python2.6. If python2.6 is called at the moment your shell loads the virtualenvwrapper.sh script, it is unhappy. Pretty straightforward.
VIRTUALENVWRAPPER_PYTHON
is made for those situations. With it, you can make sure you always use the right version of python, and don't have to always add that-p /path/to/python2.7
So, I don't agree with Stefano's answer in that case, in the OP's situation, you should have explained clearly in your .bashrc which python to use:
Like that it should be ok all the time! Virtualenvwrapper is done to simplify things.
Also, please note that
/opt/local/bin/python
must be a symlink to the version of python you select withport python select
(check that withls -l /opt/local/bin/python
to be sure).Confirmed the use of two similarly-named environment variables:
VIRTUALENVWRAPPER_PYTHON
-- which Python version is used by the virtualenvwrapper utility itself. In other words, which version of Python executesvirtualenvwrapper
, as if that Python version had been explicitly named in the#!
line of the virtualenvwrapper script file.VIRTUALENV_PYTHON
-- which Python version will be installed byvirtualenv
when you create a new virtual environment. Equivalent to-p / --python
option on thevirtualenv
command line.And perhaps obviously :) the version of Python run in a virtual environment is the version you install for that environment -- has no relation to the above environment variables after the env is created.
See https://stackoverflow.com/a/24724360/763269 for how to upgrade Python within a virtualenv.
None of these worked. I installed Python3 first when setting up my osx machine, and pip and all default to that.
First, check which python you have installed:
If this returns "Python 2.7.12", then you are set to run:
This will also activate the
api_server
workon, which changes your python executable:What does
which python
actually do? It outputs the directory of the python executables found in your PATH:By using
which python
, you are basically passing in/usr/local/bin/python
to the-p
option in the mkvirtualenv directory.What happens when you have more than one python executable returned in
which python
? Just find the one you want and pass it in:And virtualenvwrapper will end up using that python executable instead.
I know this is pretty much solved in your comments, but it's mac only,
and even more I think the correct way should be to set
VIRTUALENVWRAPPER_PYTHON
to the real python you are using on the command line.To be sure you can do
which python
.Actually, you can even do:
On linux I do this in my .bashrc, so all in all, assuming you installed virtualenv and created your first "virtual environment"
virtualenv
(how original)(and by the way, you wrote:
which is actually the opposite - virtualenv relies on using the correct python (and the packages that go with it) so it's very important to set the python path accordingly.
Even running a py file with a "#!/bin/python" might bring trouble once you are virtualenved!