"python" still runs the system version after virtualenv activate

If you are on Windows, this seemed to work for me.

Check your environment variables and move any Python path declarations in system to user environment variables.


Bottom line:

You have set "python" as a shell alias (probably in your shell startup scripts). It interferes with virtualenv's work of replacing what would be run when you type "python". Remove the alias, and you're good.

You also don't need to specify --python=/usr/local/bin/python2.7 'cuz you're using virtualenv from that Python installation, so it already uses it by default.


WFM with virtualenv 1.10.1: (see a guess further below)

$ virtualenv --python=/usr/local/bin/python2.7 testbox
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in testbox/bin/python2.7
Also creating executable in testbox/bin/python
Installing Setuptools.........................................done.
Installing Pip................................................done.
$ ls -l testbox/bin/
total 40
-rw-r--r--. 1 root root 2194 Dec  7 03:06 activate
-rw-r--r--. 1 root root 1250 Dec  7 03:06 activate.csh
-rw-r--r--. 1 root root 2389 Dec  7 03:06 activate.fish
-rw-r--r--. 1 root root 1129 Dec  7 03:06 activate_this.py
-rwxr-xr-x. 1 root root  332 Dec  7 03:06 easy_install
-rwxr-xr-x. 1 root root  340 Dec  7 03:06 easy_install-2.7
-rwxr-xr-x. 1 root root  293 Dec  7 03:06 pip
-rwxr-xr-x. 1 root root  301 Dec  7 03:06 pip-2.7
lrwxrwxrwx. 1 root root    9 Dec  7 03:06 python -> python2.7
lrwxrwxrwx. 1 root root    9 Dec  7 03:06 python2 -> python2.7
-rwxr-xr-x. 1 root root 7788 Dec  7 03:06 python2.7

And the main thing that activate does is:

PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

My guess is that you're using virtualenv that was installed for your /usr/local/bin/python2.7. That's the reason for the "Already using..." message. If that's the case, you don't need to pass --python because that virtualenv is already using it by default (check its shebang).

Still, since virtualenv creates a versionless executable and activate alters PATH, you should get /var/python_venv/testbox/bin/python as python.

  • Since python is an alias in your case, and activate doesn't use aliases - you must have it set in your bash startup scripts.

You've possibly moved/renamed a folder on the path to the venv.

venv/bin/activate contains a variable called VIRTUAL_ENV (that is used to update the path when activating a venv) and this may have a hardcoded reference to the original venv location.

VIRTUAL_ENV="/Users/<user-name>/<original-path>/venv"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

When you move/rename the folder, the outdated VIRTUAL_ENV path is added to your PATH when you activate. As a result, the first match for python will be further down the PATH and the first match will most likely be system python.

You should update these outdated hardcoded paths within the venv folder.


If you activated your virtualenv and which python gives you /usr/bin/python instead of yourvirtualenv_path/bin/python you might have a bash alias in your .bashrc or .bash_aliases files as I had.

Steps to fix it:

  1. Deactivate virtual environment
  2. To check what python is aliased to run: type python
  3. Find your python alias and delete it from ~/.bash_aliases or ~/.bashrc
  4. In my case I deleted alias python='$(which python)' from ~/.bash_aliases
  5. Delete your virtual environment
  6. Refresh your .bash files: source ~/.bashrc and source ~/.bash_aliases
  7. Recreate virtualenv
  8. Activate virtualenv
  9. Run: which python should give: yourvirtualenv_path/bin/python