簡體   English   中英

當以可執行文件./xxx.py運行程序時,設置默認的python版本

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

我想直接將python作為./xxx.py可執行文件./xxx.py

但是,當我像這樣運行它時,它將自動使用python 2.7而不是python 3。

如何使用python 3運行可執行python文件?

在這種情況下:

./magane.py shell

它會給我

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)
>>> 

但是我想要這個:

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]: 

python3使用ipython的地方。

要告訴python使用哪個版本並使它在Linux上可執行,您必須執行以下步驟:

#! 到python腳本

#!/usr/bin/env python3

添加運行權限

chmod +x xxx.py

作為腳本的第一行,您可以使用:

#!/usr/bin/python3

這樣,您可以將此行添加到編寫在Python3上運行的腳本中,否則忽略。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM