0

My default python version on my Debian 8.5 machine is 3.4.2. I want to use python 2.7 for only one project. I have tried running all of the following commands in the terminal one by one, :

virtualenv -p python2.7 env_dir
virtualenv -p python2 env_dir
virtualenv --python=python2.7 env_dir

And this is the file.py inside the env_dir:

print "Hello world from inside env_dir"

Obviously however the file.py still gets executed with python3.4.2, because i run:

python file.py

and i get:

      File "file.py", line 1
    print "Hello world from inside env_dir"
                                      ^
SyntaxError: Missing parentheses in call to 'print'

What is the problem, why it fails to run the code in python2.7?

Update

I have also tried:

virtualenv -p /usr/bin/python2.7 env_dir

source env_dir/bin/activate


(env_dir) amir@amir-debian:~/env_dir$ python file.py 
  File "file.py", line 1
    print "Insid virtual env"
                        ^
SyntaxError: Missing parentheses in call to 'print'

Running python -V after activating returns: Python 3.4.2

Update-2 Here is the process how i try to create my virtualenv and the outputs from terminal:

$ virtualenv --python=/usr/bin/python2.7 venv3

Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /home/amir/Desktop/venv3/bin/python2.7
Also creating executable in /home/amir/Desktop/venv3/bin/python
Installing setuptools, pip, wheel...done.

$ source venv3/bin/activate
(venv3) amir@amir-debian:~/Desktop$ python -V
Python 3.4.2

(venv3) amir@amir-debian:~/Desktop$ python
Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Why is the working version still 3.4.2 and not 2.7 as i gave as OPTION to vertualenv when creating venv3?

  • Have you activate (sourced) the virtualenv by also running `source env_dir/bin/activate`? Also the `-p` flag should provide the full path to the Python binary (i.e. `virtualenv -p /usr/bin/python2 env_dir`) – metatoaster Sep 18 '16 at 11:16
  • @metatoaster See the **Update** –  Sep 18 '16 at 11:27
  • 1
    You should also try deleting that old directory created with the wrong Python version; seeing you never included in the question the **full** output of everything we can't really help you. – metatoaster Sep 18 '16 at 11:42
  • I have of course deleted the old directory! What **full** output of what do you still need to understand the problem? –  Sep 19 '16 at 14:11
  • Also include the output of `python2.7 -V` before you source `venv3`, then `which python` and `which python2.7` after you have activated `venv3`, plus output of `env |grep PATH` and `mount |grep /home`. Reason why full output is not because we don't understand the problem, it is because your system has something that is interfering with normal operation of virtualenv and to figure that out we need as much information as possible to help you. – metatoaster Sep 19 '16 at 22:56

2 Answers2

0

Check if you have python2 by doing python -v if you do have it, you can do python2 file.py

There's also an answer from here

which is virtualenv -p /usr/bin/python2.7 <path/to/new/virtualenv/>

Community
  • 1
  • 1
user3145912
  • 131
  • 1
  • 15
0

You can specify the version of python to use by doing

$ virtualenv venv --python=/usr/bin/python2

Tasdik Rahman
  • 2,160
  • 1
  • 25
  • 37