17

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?

5
  • 2
    How's you zshrc or bashrc configured? Commented Apr 28, 2015 at 6:38
  • The PATH variable sets automatically when you activate the env. Try virtualenv -p /usr/bin/python2.7 <path/to/new/virtualenv/> Commented Apr 28, 2015 at 6:38
  • I did not configure any python in the bashrc profile. But one werid thing is that when I'm in the the project root and run: "python manage.py shell", it uses the system level's python. But when I simple run "manage.py shell", it uses the correct version (the one installed in the virtualenv)!! Why would this happen? Commented Apr 28, 2015 at 6:47
  • Where is python2.7 executable located? if you are using ubuntu then there wouldn't be any problem. run which python2 outside the virtualenv. Commented Apr 28, 2015 at 7:42
  • it's just located in /usr/bin Commented Apr 28, 2015 at 7:56

8 Answers 8

11

If you changed the path to your venv or ranamed any of the parents folders of your venv directory, then this will break the configured paths, if that is case you have two options:

  1. recreating it

    • Create a requirements.txt file using: pip freeze > requirements.txt

    • Delete the venv directory: rm -r old-vnev/

    • Create a new virtualenv with correct name: python -m venv new-venv
    • Activate new virtualenv: source new-venv/bin/activate
    • Install packages from requirements.txt: pip install -r requirements.txt
  2. Another simpler way

    • search for all occurences of the string old/path/to/your/venv/
    • replace them with correct/path/to/your/venv/

after that source new-venv/bin/activate will work as intended again.

Hope this help!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this! There's one gotcha though: If you don't have a requirements.txt and your virtualenv is configured for an interpreter that doesn't exist on your current system (for example, if your venv folder comes from a repo), you'll need to modify the pyvenv.cfg file to point at an interpreter you do have on your system in order to create the requirements file. An easy way to do this is to create a new environment, copy its pyvenv.cfg file to the old venv directory (and backup the original pyvenv!), then generate the requirements file as you describe.
7

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

3 Comments

is there a way to solve this problem without changing any path variables?I did not configure any python in the bashrc profile. But one werid thing is that when I'm in the the project root and run: "python manage.py shell", it uses the system level's python. But when I simple run "./manage.py shell", it uses the correct version (the one installed in the virtualenv)!! Why would this happen?
if you are not ready to change path variables, why dont you create new env ?Also check bin/activate script.You can find there
@itzMEonTV, Thank you for your answer. But shouldn't export OLD_PYTHONPATH="$PYTHONPATH" come before export PYTHONPATH="/path/to/python" ?
6

I discovered the same problem...

and like @skyline75489 mentioned:

I forgot that i had stated an alias to my python3 executable a time ago.
I found it in my .bash files in my home directory and removed it.
Everything worked out fine again with my virtual environment.

Comments

4

In case it helps anyone else: if you changed the path to your venv folder (such as changing the parent folder), this will happen. This was my issue.

Recreating your virtualenv will fix it, as you should hopefully have a requirements.txt created to rebuild your virtualenv.

This might have even been the root cause for OP.

Comments

0

Double check your paths. I had an issue like this recently where running which python from within the activated virtualenv would still return the default system version (/usr/bin/python). However, if I ran the scripts specifying the binaries directly (./venv/bin/python, etc) from within the virtualenv, it worked as expected so it appeared all the dependencies had been installed correctly.

The issue was that I had moved the parent virtualenv directory after building everything. This meant all the virtualenv paths pointed to the original location which was no longer valid, and python correctly defaulted to the default system binary.

Comments

0

I use a bash script like this:

$ source venv/bin/activate
$ alias vpython=$VIRTUAL_ENV/bin/python3

and use vpython when wanting to use the python executable within the virtual environment. A nice way to check which executable you are actually using within python is the following:

>>> import sys
>>> print(f'executable \033[0;33;40m{sys.executable}\033[0m')

Comments

0

In my situation after system update symbolic link from the virtualenv was somehow broken and it switched to default system python version. The solution was to replace symbolic link by the correct one.

  1. Deactivate virtual env if you are inside by:

    deactivate

  2. Change virtualenv python symbolic link:

    ln -s /your/wanted/python/bin/python /your/virtualenv/bin/python

  3. Start virtualenv again and it should use correct python version.

If you are not sure where is your python, then you can localise it by: which python3

Comments

0

I had a similar problem. But I had it because I had moved my env folder to another place. So, if you did so, just go to activate file in bin folder and change VIRTUAL_ENV="CurrentPathToYourEnvFolder" (it's 40th line in file)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.