Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"python" still runs the system version after virtualenv activate

I installed Python 2.7 with these commands:

./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall

Then I created the virtualenv to point to the 2.7 installation:

$ virtualenv --python=/usr/local/bin/python2.7 testbox
Already using interpreter /usr/local/bin/python2.7
New python executable in /var/python_venv/testbox/bin/python2.7
Also creating executable in /var/python_venv/testbox/bin/python
Installing setuptools, pip, wheel...done.
$ source testbox/bin/activate
(testbox) $ python
Python 2.6.6 ( , Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

But if I activate it its point to 2.6.6:

cat /etc/redhat-release
CentOS release 6.6 (Final)

EDIT:

I am not sure why this doesn't work, but I can still use the virtualenv in my Apache Django app, so I am not too concerned.

like image 569
Jabda Avatar asked Dec 06 '16 20:12

Jabda


3 Answers

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.

like image 141
Thom Lane Avatar answered Nov 16 '22 02:11

Thom Lane


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.
like image 41
ivan_pozdeev Avatar answered Nov 16 '22 01:11

ivan_pozdeev


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

like image 20
jturi Avatar answered Nov 16 '22 01:11

jturi