我想从Sublime Text 3运行Python代码,但不确定该如何操作。即使只是从控制台运行也可以接受。有人知道怎么做吗?
我想从Sublime Text 3运行Python代码,但不确定该如何操作。即使只是从控制台运行也可以接受。有人知道怎么做吗?
工具->构建系统->Python 或 Ctrl+B
{
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "D:\\.python_venvs\\general_python\\Scripts\\python.exe -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "D:\\.python_venvs\\general_python\\Scripts\\python.exe -m py_compile \"${file}\"",
}
]
}
对于 Mac/Linux,不要忘记更改Python的路径:
{
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "/home/<user>/.python_venvs/general_python/Scripts/python -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "/home/<user>/.python_venvs/general_python/Scripts/python -m py_compile \"${file}\"",
}
]
}
工具 ▶ 构建系统 ▶ Python Custom 6. 执行您的代码: Ctrl/CMD + B
这篇回答是给那些想在Sublime中运行Python脚本的谷歌用户。正如其他回答所解释的,你所需要的只是一个Sublime构建系统,在经过一番努力后,我已经成功地将其应用于Linux系统。
{
"cmd": ["gnome-terminal", "--", "/bin/bash", "-c", "python3 -u \"$file\" echo;echo;echo Press Enter to exit...;read"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
我认为这是迄今为止最简单的方法。希望能对你有所帮助。
当您使用Ctrl + B时,Sublime Text 3将在集成控制台中运行您的Python代码。
如果您想在自己的终端上运行代码,但仍然希望在集成控制台中获得一些错误信息,则需要构建自己的生成器或使用插件。
@Thayakorn Rakwetpakorn的答案是正确的
按Ctrl + B,确保在尝试运行之前将文件保存为hello.py或其他名称
如果这样不起作用 工具->构建系统->新建构建系统
删除现有代码并粘贴我下面展示的代码
{
//"shell_cmd": "make"
"cmd": ["C:\\Users\\Programs\\Python\\Python37\\python.exe","-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
然后在上面的代码中将文件位置更改为您的python.exe文件位置所在的位置
"C:\\Users\\Programs\\Python\\Python37\\python.exe"
不要使用我的文件路径的代码行