[英]Possible to switch between python 2 and 3 in Sublime Text 3 build systems? (Windows)
我當前所有的編碼都在 python 3 中,我是通過 Anaconda 包安裝的。 但是,我需要在 python 2 中同時處理一些代碼。有沒有辦法在 Sublime 中添加構建系統,以便我可以在兩者之間流暢地切換? 我同時安裝了 python 2 和 3,但是看不到簡單地編輯構建系統以在兩種語言之間切換的方法。 我用於 python 3 的構建系統是:
{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
非常感謝!
指定 Python 版本的絕對路徑。 例如,如果 Python 版本 3 位於/usr/bin/python3
並且 Python 版本 2 位於/usr/bin/python2
:
Python 3 構建配置:
{
"cmd": ["/usr/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
Python 2 構建配置:
{
"cmd": ["/usr/bin/python2", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
查找通過Python的位置, which
:
$ which python
/usr/bin/python
$ which python3
/usr/bin/python3
$ which python2
/usr/bin/python2
請參閱Sublime Text 3 的構建系統執行不同版本的 PHP ,它是相同的原理。
老問題,但是...
在 Windows 上,您不需要創建新的構建系統或指定絕對路徑。 編輯現有的 python 構建系統。
SublimeText\\Data\\Packages\\
創建文件夾Python
-> 像這樣SublimeText\\Data\\Packages\\Python\\
Python.sublime-build
,它將覆蓋現有的 python 構建系統。在此文件中寫入( python 啟動器文檔):
{ "shell_cmd": "py -u \\"$file\\"", "file_regex": "^[ ]*File \\"(...*?)\\", line ([0-9]*)", "selector": "source.python", "env": {"PYTHONIOENCODING": "utf-8"}, }
對於選擇版本的python解釋器,在腳本的第一行寫( shebang行)
#! python
#! python
或#! python3
1、安裝pyhton3: https ://www.python.org/downloads/
2、找到文件“python.exe”並重命名為“python3.exe”
3、在Windows命令行中:
where python3.exe
將這個 ubication 保存在任何你想要的地方。
4、在sublime goto -> Tools -> Build system -> New build system(這樣打開一個文件)
5、最后是3號的ubication文件,只更改ubication:
{
/*replace this*/"cmd": ["C:/Users/youruser/AppData/Local/Programs/Python/Python37-32/python3.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
將文件保存為“python3”。
6、在sublime中去工具->構建系統->選擇python3,你可以在python 3中運行這段代碼。
7、顯示你的python版本:
import sys
print(sys.version)
如果你想在python 2中運行,那么去工具->構建系統->選擇“Python”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.