Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtualenv doesn't use right version of Python

I'm working in Amazon's Cloud9.

ec2-user:~/environment/flask_init $ python -V
Python 2.7.14
ec2-user:~/environment/flask_init $ virtualenv -p python3 venv
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/ec2-user/environment/flask_init/venv/bin/python3
Also creating executable in /home/ec2-user/environment/flask_init/venv/bin/python
Installing setuptools, pip, wheel...done.
ec2-user:~/environment/flask_init $ source venv/bin/activate
(venv) ec2-user:~/environment/flask_init $ python -V
Python 2.7.14

Why is the virtual environment not using Python 3?

Please note that this question is not a duplicate of this one. The issue was specifically to do with the way the Cloud 9 environment sets up Python alias.

like image 740
RubyNoob Avatar asked Oct 20 '18 06:10

RubyNoob


People also ask

How do I use a specific 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 downgrade Python version in virtualenv?

You need to uninstall the existing python version and re-install the required python version and set up your environment.

Can I upgrade Python version in virtualenv?

Can I upgrade Python in a VENV? You can't upgrade to a Python version you don't already have on your system somewhere, so make sure to get the version you want, first, then make all the venvs you want from it.


2 Answers

I tried your flow on my machine and everything works as expected.

dluzak@Karol-PC:/tmp$ python -V
Python 2.7.12
dluzak@Karol-PC:/tmp$ virtualenv -p python3 venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /tmp/venv/bin/python3
Also creating executable in /tmp/venv/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
dluzak@Karol-PC:/tmp$ source venv/bin/activate
(venv) dluzak@Karol-PC:/tmp$ python -V
Python 3.5.2
(venv) dluzak@Karol-PC:/tmp$ 

Nonetheless I personally use virtualenv as module when creating venv with python 3: python3 -m virtualenv venv. Maybe this would work.

You provided very little details. Have you installed virtualenv for both Python 2 and 3? Are you sure Python 3 interpreter works fine?

Edit:

After investigation in comments we found out that the problem was in bash settings configured by Amazon. It seams that Amazon configures bash (probably in ~/.bashrc) to replace python calls with an alias. To fix this a call unalias python before enabling venv is needed. It is described in Amazon docs

like image 76
Dluzak Avatar answered Oct 22 '22 16:10

Dluzak


When I was using virtualenv earlier today, I had the same problem that my env was not using the right version of python.

Instead of activating my environment like this:

source activate

I found that activating it like this actually worked:

source ./activate

Hope this is helpful!

like image 38
Jude Avatar answered Oct 22 '22 18:10

Jude