6

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.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
RubyNoob
  • 517
  • 1
  • 7
  • 17
  • 1
    could you try giving the full path to python3 while creating virtualenv like `virtualenv -p path/to/python3 venv` – ansu5555 Oct 20 '18 at 06:19
  • Still doesn't work when I create using the link `/usr/bin/python3` – RubyNoob Oct 20 '18 at 14:29
  • @RubyNoob Could you please try creating the venv again with `-v` flag and paste the logs into the question? – Dluzak Oct 20 '18 at 15:04
  • Everything looks okay in your output, could you try executing `python3 -v` and if the output is `Python 3.x` then try running `python3 -m virtualenv venv` – ansu5555 Oct 20 '18 at 15:20
  • @Dluzak Stack Overflow says there's too much code in the edit when I enter the logs, so I pasted it here: (https://pastebin.com/dSzuM73a) – RubyNoob Oct 20 '18 at 15:23
  • @RubyNoob Do you have a shell alias named `python`? Can you try commands: `which python`; `$(which python) --version`? – phd Oct 20 '18 at 19:41
  • Possible duplicate of [Use different Python version with virtualenv](https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv) – Julian Camilleri Oct 23 '18 at 08:28

4 Answers4

4

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

Dluzak
  • 315
  • 1
  • 11
  • thanks for the suggestions. Python 3 interpreter runs and shows v3.6.5. `python3 -m pip install virtualenv` gives me `Requirement already satisfied: virtualenv in /usr/local/lib/python3.6/site-packages`. I tried your module approach but the virtual environment is still running Python 2. – RubyNoob Oct 20 '18 at 14:49
  • It's almost as though the virtual environment isn't running, because when I am in it and run python3 it opens the python3 interpreter. So it seems to have two pythons and is using python3 by default, just like the host environment without the virtual environment running. – RubyNoob Oct 20 '18 at 14:52
  • @RubyNoob I don't quite understand your second comment. So after activating virtualenv, when calling python3 you are getting python3, that is: exactly what you wanted? If in doubt if it is the host or virtualenv one you can call `which python3` – Dluzak Oct 20 '18 at 14:58
  • Python3 is definitely the virtual environment's version: `~/environment/flask_init/venv/bin/python3` - but I want this virtual environment to use python 3 by default, and right now it's using python 2.7 by default. I didn't want Python 2 at all in this venv. – RubyNoob Oct 20 '18 at 15:26
  • @RubyNoob `venv/bin/python` should be symlink pointing to the `venv/bin/python3`. Could you check if thats the case? The easiest way would be to call `ls -alF ~/environment/flask_init/venv/bin`. – Dluzak Oct 20 '18 at 15:34
  • @RubyNoob You can also check if after activating the venv the PATH has the `~/environment/flask_init/venv/bin` directory as the first element with `echo $PATH` – Dluzak Oct 20 '18 at 15:37
  • Thanks @Dluzak. I ran that and here are the results. (https://pastebin.com/31Jx2iwM). It seems to be pointing to python 3 binaries, right? – RubyNoob Oct 20 '18 at 15:39
  • Yup, that directory is first in the $PATH. But how do I get the virtual environment to use Python3 by default? – RubyNoob Oct 20 '18 at 15:40
  • @RubyNoob Have you tried `which python` after activating the venv? – Dluzak Oct 20 '18 at 16:12
  • @Dluzac `alias python='python27'` `/usr/bin/python27`. I wonder if this is just something to do with how Amazon has Cloud9 set up. I've had no problems with virtualenv on local machines in the past. – RubyNoob Oct 20 '18 at 16:26
  • Ok, now it makes sense. The problem is in your bash settings. It seams that Amazon configured your bash (probably in ~/.bashrc) to replace `python` calls with this alias. If you call `unalias python` before enabling venv, everything should work. It is even included in [Amazon docs](https://docs.aws.amazon.com/cloud9/latest/user-guide/sample-python.html) – Dluzak Oct 20 '18 at 16:54
  • Nailed it! Thanks so much, @Dluzak. – RubyNoob Oct 21 '18 at 04:09
1

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!

Jude
  • 56
  • 1
  • 4
1

Here is how i create virtualenv on Cloud9

Python 3.4

$ sudo pip install virtualenv
$ virtualenv -p /usr/bin/python3.4 venv
$ source venv/bin/activate

Python 3.6

$ sudo apt update
$ sudo apt install python3.6-venv
$ python3.6 -mvenv venv
$ source venv/bin/activate
0

I have encountered a similar issue. In my case did not work because I moved the virtual env folder (but the same thing happens when you rename it).

You can understand which version of python (and thus which module will import) is using by typing

$ which python

If it write something like:

/usr/bin/python

Then it means your virtual env is not being activated.
To solve this issue, instead of creating a new virtual environment, you can simply edit the script activation file in your env:

$ nano venv/bin/activate

And edit the following line with your absolute path of your virtual environment:

VIRTUAL_ENV="/YOUR_ABSOLUT/PATH_TO/venv"

Hope it helps :)