1

I have downloaded a python program from git. This program is python 3. On my laptop i have both python 2.7 and python 3.4. Python 2.7 is default version. when i want run this program in terminal it gives some module errors because of it used the wrong version. how can i force an name.py file to open in an (non) default version of python.

I have tried so search on google but this without any result because of lack of search tags. also just trying things like ./name.py python3 but with same result(error)

Jan-Bert
  • 921
  • 4
  • 13
  • 22
  • 4
    `python3 name.py` might work. If not you might want to have a look into 'python virtual environments' (google it) which allows you to set up a self-contained environment with specific versions of python, packagers etc, in which you could make the default version of python 3.4 – Tom Dalton Jan 27 '16 at 22:08
  • python3 nameofthescript.py – n1c9 Jan 27 '16 at 22:08
  • If that does not work, try `/full/path/to/python3 script.py` – tobias_k Jan 27 '16 at 22:08

3 Answers3

0

When you type "python", your path is searched to run this version. But, if you specify the absolute path of the other python, you run it the way you want it.

Here, in my laptop, I have /home/user/python3_4 and /home/user/python2_7. If I type python, the 3.4 version is executed, because this directory is set in my path variable. When I want to test some scripts from the 2.7 version, I type in the command line: /home/user/python2_7/bin/python script.py. (Both directory were chosen by me. It's not the default for python, of course).

I hope it can help you.

Paulo
  • 1,458
  • 2
  • 12
  • 26
0

Use the shebang #!<path_to_python_version_you_want>

As in:

#!/usr/bin/env python

at the very top of your .py file

Also checkout: Should I put #! (shebang) in Python scripts, and what form should it take?

Community
  • 1
  • 1
clipsett
  • 13
  • 8
0

The Method of @Tom Dalton and @n1c9 work for me! python3 name.py

Jan-Bert
  • 921
  • 4
  • 13
  • 22