Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtualenv uses wrong python, even though it is first in $PATH

I had a problem where python was not finding modules installed by pip while in the virtualenv.

I have narrowed it down, and found that when I call python when my virtualenv in activated, it still reaches out to /usr/bin/python instead of /home/liam/dev/.virtualenvs/noots/bin/python.

When I use which python in the virtualenv I get:

/home/liam/dev/.virtualenvs/noots/bin/python

When I look up my $PATH variable in the virtualenv I get:

bash: /home/liam/dev/.virtualenvs/noots/bin:/home/liam/bin:/home/liam/.local/bin:/home/liam/bin:/home/liam/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin: No such file or directory

and yet when I actually run python it goes to /usr/bin/python

To make things more confusing to me, if I run python3.5 it grabs python3.5 from the correct directory (i.e. /home/liam/dev/.virtualenvs/noots/bin/python3.5)

I have not touched /home/liam/dev/.virtualenvs/noots/bin/ in anyway. python and python3.5 are still both linked to python3 in that directory. Traversing to /home/liam/dev/.virtualenvs/noots/bin/ and running ./python, ./python3 or ./python3.5 all work normally.

I am using virtualenvwrapper if that makes a difference, however the problem seemed to occur recently, long after install virtualenv and virtualenvwrapper

like image 201
liamhawkins Avatar asked Jan 07 '17 17:01

liamhawkins


People also ask

How do I change the version of Python in virtualenv?

By default, that will be the version of python that is used for any new environment you create. However, you can specify any version of python installed on your computer to use inside a new environment with the -p flag : $ virtualenv -p python3. 2 my_env Running virtualenv with interpreter /usr/local/bin/python3.

Can I install different Python version in virtualenv?

virtualenv supports older Python versions and needs to be installed using the pip command. In contrast, venv is only used with Python 3.3 or higher and is included in the Python standard library, requiring no installation.


2 Answers

My problem was that i recently moved my project with virtualenv to another location, due to this activate script had wrong VIRTUAL_ENV path.

$ cat path_to_your_env/bin/activate  ... # some declarations  VIRTUAL_ENV="/path_to_your_env/bin/python"  # <-- THIS LINE export VIRTUAL_ENV  ... # some declarations 

To fix this, just update VIRTUAL_ENV in activate script.

Also you maybe need to fix first line of your bin/pip to link to real python path.

like image 119
vishes_shell Avatar answered Sep 22 '22 00:09

vishes_shell


As tdelaney suggested in the comments, I ran alias and found that I had previously aliased python to /usr/bin/python3.5 in my .bashrc.

I removed that alias from my .bashrc, ran unalias python, and source ~/.bashrc and the problem was solved.

like image 22
liamhawkins Avatar answered Sep 22 '22 00:09

liamhawkins