简体   繁体   中英

Run python program from terminal

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)

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.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM