如何創建在崇高的文本3構建系統,其中"cmd"
被替換,如果它存在家當?如何創建崇高的文本3構建系統讀取家當
更具體地說,是有沒有辦法改變的Python構建系統使用的家當指定的Python版本,並且使用默認的,如果沒有家當存在?
如何創建在崇高的文本3構建系統,其中"cmd"
被替換,如果它存在家當?如何創建崇高的文本3構建系統讀取家當
更具體地說,是有沒有辦法改變的Python構建系統使用的家當指定的Python版本,並且使用默認的,如果沒有家當存在?
Sublime構建系統有一個名爲target
的選項,該選項指定要調用以執行構建的WindowCommand
。默認情況下,這是內部exec
命令。您可以創建自己的命令,將檢查的家當的文件,並使用該解釋或一些默認的除外。
例如(警告:我不是在Python超級精通所以這可能是相當難看):
import sublime, sublime_plugin
class ShebangerCommand(sublime_plugin.WindowCommand):
def parseShebang (self, filename):
with open(filename, 'r') as handle:
shebang = handle.readline().strip().split (' ', 1)[0]
if shebang.startswith ("#!"):
return shebang[2:]
return None
def createExecDict(self, sourceDict):
current_file = self.window.active_view().file_name()
args = dict (sourceDict)
interpreter = args.pop ("interpreter_default", "python")
exec_args = args.pop ("interpreter_args", ["-u"])
shebang = self.parseShebang (current_file)
args["shell_cmd"] = "{} {} \"{}\"".format (shebang or interpreter,
" ".join (exec_args),
current_file)
return args
def run(self, **kwargs):
self.window.run_command ("exec", self.createExecDict (kwargs))
你會在Packages/User
保存這是一個Python文件(例如shebanger.py
)。
這將創建一個名爲shebanger
的新命令,它收集它所給出的參數,檢查構建觸發的窗口的當前活動視圖中的文件,以查看第一行是否是shebang,然後綜合參數所需exec
命令並運行它。
由於默認的python編譯系統假定它正在構建當前文件並將-u
作爲參數傳遞,因此該命令也複製該內容。但是請注意,這個代碼是不是100%正確的,因爲在家當行任何參數將被忽略,但你的總體思路。
在使用中,您將修改默認Python.sublime-build
文件看起來像這樣:
{
// WindowCommand to execute for this build
"target": "shebanger",
// Use this when there is no shebang
"interpreter_default": "python",
// Args to pass to the interpreter
"interpreter_args": ["-u"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"interpreter_args": ["-m py_compile"],
}
]
}
注意的是,在變型,我們覆蓋的解釋變量是什麼;如果需要,也可以覆蓋那裏的默認解釋器。
如果認爲使用標準.sublime-build
文件執行此操作的唯一方法是將您的文件傳遞給另一個腳本,然後解析shebang並將其傳遞到正確的Python版本。
或者,你可以指定build variants,但你必須手動選擇所需構建變量。
我python.sublime建造
{
"cmd": ["py", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell":true
}
在windows我用PY發射器根據家當
這看起來相當檢測版本有希望 - 我讓你知道如何去。謝謝! –