简体   繁体   中英

set default python version when run the program as executable ./xxx.py - on linux

I would like to run the python directly as executable file like ./xxx.py

However, when I run it like this, it will automatically use python 2.7 instead of python 3.

How could I use python 3 to run the executable python file?

In the case:

./magane.py shell

It will give me

Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 

But I would like to have this:

python3 manage.py shell
/home/cliu/.local/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py:57: RuntimeWarning: SQLite received a naive datetime (2015-05-31 15:24:46.785297) while time zone support is active.
  RuntimeWarning)

Python 3.4.3 (default, Mar 26 2015, 22:03:40) 
Type "copyright", "credits" or "license" for more information.

IPython 2.3.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: 

where python3 uses ipython.

To tell python what version to use and to make it executable on Linux you have to do the following steps:

Add #! to the python script

#!/usr/bin/env python3

Add permissions to run it

chmod +x xxx.py

As the first line of our script you may use :

#!/usr/bin/python3

In this way you can add this line to the scripts which are written to be ran on Python3 and ignore otherwise.

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