1.在文件首行添加以下代碼:python
若是你想使用默認python版本執行當前的腳本,即python2,則須要添加spa
#!/usr/bin/env python # -*- coding: utf-8 -*-
若是你想使用python3執行當前腳本,則須要添加.net
#!/usr/bin/env python3 # -*- coding: utf-8 -*-
2.將python文件的擴展名改成command。如:code
mv test.py test.command
3.爲腳本添加可執行權限blog
chmod +x test.command
4.完成上面3步,雙擊腳本就能夠自動執行了。utf-8
注意:get
若是你的python文件中須要使用文件當前的相對路徑,須要先切換到相對路徑後,才能夠繼續使用相對路徑。class
解決的辦法是:test
1.導入系統庫import
import sys
2.使用python代碼切換到當前目錄下
#切換到當前目錄 python3的寫法,python2的寫法請自行搜索 os.chdir(sys.path[0])
參考連接: