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.
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.
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
.
python
is an alias in your case, and activate
doesn't use aliases - you must have it set in your bash
startup scripts.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:
type python
~/.bash_aliases
or ~/.bashrc
alias python='$(which python)' from ~/.bash_aliases
source ~/.bashrc
and source ~/.bash_aliases
which python
should give: yourvirtualenv_path/bin/python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With