Warm tip: This article is reproduced from serverfault.com, please click

"python" still runs the system version after virtualenv activate

发布于 2016-12-06 20:02:50

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 teh 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 doesnt work, but I can still use the virtualenv in my apache DJANGO app so I am not too concerned

Questioner
Jabda
Viewed
0
ivan_pozdeev 2017-10-26 15:12:41

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.