Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python virtualenv does not use correct version of python

I'm creating a Django app that requires me to use python2.7.6 . My system has python3.4.1 installed so I have to use a virtualenv with python2.7 installed. I installed such a virtualenv using Pycharm and named it django_python_2.7 but when I activate it in the terminal and run "python", it still shows that it's using system's python3.4.1: here is what I did:

  1. Activate the environment:

    source django_python_2.7/bin/activate

  2. Run python, and it shows:

    Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21) ---> this is the system level python and not the one installed in virtualenv

However, when I run which python, it shows the correct path that points to virtualenv's python version:

/Users/calvinmwhu/....../django_python_2.7/bin/python

When I explicitly run the python version installed in that virtualenv:

django_python_2.7/bin/python

it shows the correct version:

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 

I have no idea what's going on. I'm developing this app in Pycharm IDE but I really like executing commands in the terminal . But in the terminal the virtualenv is not using the correct version of python..Why does running a simple "python" command in the virtualenv still default to the system's python ?

Could anyone provide some hints? Is it necessary to change the PATH variable to make it contain the path to the virtualenv's python?

like image 527
Calvin Hu Avatar asked Apr 28 '15 06:04

Calvin Hu


1 Answers

If you want to change the PYTHONPATH used in a virtualenv, you can add the following line to your virtualenv's django_python_2.7/bin/activate file

export PYTHONPATH="/path/to/python"
export OLD_PYTHONPATH="$PYTHONPATH"

To restore to its original value on deactivate, you could add following line to your django_python_2.7/bin/postdeactivate script.

export PYTHONPATH="$OLD_PYTHONPATH"

Otherwise, create new env using

virtualenv -p /usr/bin/python2.7 django_python_2.7
like image 60
itzMEonTV Avatar answered Sep 28 '22 03:09

itzMEonTV