使用 Sublime Text 3 设置 Python 3 构建系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23730866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set up Python 3 build system with Sublime Text 3
提问by user3002473
I want to configure Sublime Text 3 to build Python 3, but I don't seem to understand how the builds work. Many tutorials have told me to make a build file containing code such as:
我想配置 Sublime Text 3 来构建 Python 3,但我似乎不明白构建是如何工作的。许多教程告诉我制作一个包含代码的构建文件,例如:
{
'cmd': ['/usr/bin/python3', '-u', '$file'],
'file_regex': '^[ ]*File "(…*?)", line ([0-9]*)',
'selector': 'source.python'
}
and save it as a file called Python.sublime-build
or python3.sublime-build
(much of the information I found was conflicting). One tutorial suggested creating a new folder in the ST3 Packages folder called Python
and add the build file in there, whilst other tutorials suggested leaving it in the folder called User
.
并将其保存为一个名为Python.sublime-build
or的文件python3.sublime-build
(我发现的大部分信息都是相互矛盾的)。一个教程建议在 ST3 Packages 文件夹中创建一个名为的新文件夹Python
并将构建文件添加到其中,而其他教程建议将其保留在名为User
.
One tutorial explained how I had to change the Environment Variable path
on my operating system to get it to work. That didn't seem to help either.
一个教程解释了我必须如何更改path
操作系统上的环境变量才能使其工作。这似乎也没有帮助。
I added a folder Python
to Packages
(since it wasn't there already) and added in a build file with the name Python.sublime_build
which featured only the code I posted above in it. Now when I attempt to run Sublime Text it gives me this error:
我添加了一个文件夹Python
到Packages
(因为它是不存在的话),并在名为构建文件添加Python.sublime_build
该功能只有我上面贴在它的代码。现在,当我尝试运行 Sublime Text 时,它给了我这个错误:
Error trying to parse build system:
Expected value in Packages\Python\Python.sublime-build:2:5
采纳答案by MattDMo
The reason you're getting the error is that you have a Unix-style path to the python
executable, when you're running Windows. Change /usr/bin/python3
to C:/Python32/python.exe
(make sure you use the forward slashes /
and not Windows-style back slashes \
). Once you make this change, you should be all set.
您收到错误的原因是python
当您运行 Windows 时,您有一个 Unix 风格的可执行文件路径。更改/usr/bin/python3
为C:/Python32/python.exe
(确保使用正斜杠/
而不是 Windows 风格的反斜杠\
)。进行此更改后,您应该已准备就绪。
Also, you need to change the single quotes '
to double quotes "
like so:
此外,您需要将单引号更改'
为双引号,"
如下所示:
{
"cmd": ["c:/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
The .sublime-build
file needs to be valid JSON, which requires strings be wrapped in double quotes, not single.
该.sublime-build
文件需要是有效的JSON,这要求字符串用双引号括起来,而不是单引号。
回答by DmitryRom
If you are using PyQt, then for normal work, you should add "shell":"true" value, this looks like:
如果您使用的是 PyQt,那么对于正常工作,您应该添加 "shell":"true" 值,如下所示:
{
"cmd": ["c:/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell":"true"
}
回答by Daniel
Steps to Make Sublime Text a Python IDE (Windows)
使 Sublime Text 成为 Python IDE 的步骤 (Windows)
Tested successfully on Sublime Text 3. Assuming Sublime Text and package control are already installed . . .
在 Sublime Text 3 上测试成功。假设已经安装了 Sublime Text 和包控件。. .
Install Python (python.org) and pay attention to where it is installed or choose a simple location like the C drive, agreeing to remove character limit at the end of the installation.
Install package SublimeREPL (Cntrl + Shift + P, Package Control - Install Package, SublimeREPL, Enter).
Go to Preferences, Package Settings, SublimeREPL, Settings - User.
Paste in the following, updating the file path to your python installation folder, as needed. You may customize these and choose whatever syntax you like (last line) but I prefer my output in plain text.
{ "default_extend_env": {"PATH":"C:\Program Files\Python36\"}, "repl_view_settings": { "translate_tabs_to_spaces": false, "auto_indent": false, "smart_indent": false, "spell_check": false, "indent_subsequent_lines": false, "detect_indentation": false, "auto_complete": true, "line_numbers": false, "gutter": false, "syntax": "Packages/Text/Plain text.tmLanguage" } }
Save and close the file (SublimeREPL.sublime-settings).
Go to Tools, Build System, New Build System.
Replace all existing text with the following:
{ "target": "run_existing_window_command", "id": "repl_python_run", "file": "config/Python/Main.sublime-menu" }
Cntrl + S or save as "C:\Users[username]\AppData\Roaming\Sublime Text 3\Packages\User\SublimeREPL-python.sublime-build" updating username or path as needed. This should be wherever your settings and builds are stored by Sublime Text.
Go to Tools, Build System, select SublimeREPL-python.
All done--now to test. Open or create a simple python file, having a *.py extension and save it wherever desired.
Make sure the file is open and selected in Sublime Text. Now, when you press Cntrl + B to build and run it, it will open another tab, titled "REPL[python]", executing and displaying the results of your python code.
安装 Python (python.org) 并注意安装位置或选择 C 盘等简单位置,在安装结束时同意取消字符限制。
安装包 SublimeREPL(Cntrl + Shift + P,包控制 - 安装包,SublimeREPL,回车)。
转到首选项,包设置,SublimeREPL,设置 - 用户。
粘贴以下内容,根据需要更新 python 安装文件夹的文件路径。您可以自定义这些并选择您喜欢的任何语法(最后一行),但我更喜欢我的纯文本输出。
{ "default_extend_env": {"PATH":"C:\Program Files\Python36\"}, "repl_view_settings": { "translate_tabs_to_spaces": false, "auto_indent": false, "smart_indent": false, "spell_check": false, "indent_subsequent_lines": false, "detect_indentation": false, "auto_complete": true, "line_numbers": false, "gutter": false, "syntax": "Packages/Text/Plain text.tmLanguage" } }
保存并关闭文件 (SublimeREPL.sublime-settings)。
转到工具,构建系统,新建构建系统。
将所有现有文本替换为以下内容:
{ "target": "run_existing_window_command", "id": "repl_python_run", "file": "config/Python/Main.sublime-menu" }
Cntrl + S 或另存为 "C:\Users[username]\AppData\Roaming\Sublime Text 3\Packages\User\SublimeREPL-python.sublime-build" 根据需要更新用户名或路径。这应该是你的设置和构建存储在 Sublime Text 的任何地方。
转到工具,构建系统,选择 SublimeREPL-python。
全部完成——现在进行测试。打开或创建一个简单的 python 文件,扩展名为 *.py 并将其保存在任何需要的地方。
确保文件已打开并在 Sublime Text 中选择。现在,当您按 Cntrl + B 构建并运行它时,它将打开另一个名为“ REPL[python]”的选项卡,执行并显示您的 Python 代码的结果。
If you would like to go a step further, I highly recommend making the follow changes, to allow Sublime to reload your executed python in the same window, when you press Cntrl+B (Build), instead of it opening a new tab each time:
如果您想更进一步,我强烈建议您进行以下更改,以允许 Sublime 在同一窗口中重新加载您执行的 python,当您按下 Cntrl+B (Build) 时,而不是每次打开一个新选项卡:
Add the following line in the "repl_python_run" command in (Preferences, Browse Packages) SublimeREPL\config\Python\Main.sublime-menu, right before the "external_id": "python" argument:
在 (Preferences, Browse Packages) SublimeREPL\config\Python\Main.sublime-menu 的 "repl_python_run" 命令中添加以下行,就在 "external_id": "python" 参数之前:
"view_id": "*REPL* [python]",
and then to change the line:
然后更改行:
if view.id() == view_id
into:
进入:
if view.name() == view_id
in SublimeREPL\sublimerepl.py.
在 SublimeREPL\sublimerepl.py 中。
回答by Shubham Kumar
Run Python Files in Sublime Text3
在 Sublime Text3 中运行 Python 文件
For Sublime Text 3, First Install Package Control:
对于 Sublime Text 3,首先安装包控件:
- Press Ctrl + Shift + P, a search bar will open
Type Install package and then press enter Click here to see Install Package Search Pic
After the package got installed. It may prompt to restart SublimeText
- After completing the above step
- Just again repeat the 1st and 2nd step, it will open the repositories this time
- Search for Python 3 and Hit enter.
- There you go.
- Just press Ctrl + B in your python file and you'll get the output. Click here to see Python 3 repo pic
- 按 Ctrl + Shift + P,将打开搜索栏
输入安装包然后按回车 点击这里查看安装包搜索图片
安装包后。可能会提示重启 SublimeText
- 完成以上步骤后
- 再次重复第一步和第二步,这一次它会打开存储库
- 搜索 Python 3 并按回车键。
- 你去吧。
- 只需在 python 文件中按 Ctrl + B 即可获得输出。 单击此处查看 Python 3 存储库图片
It perfectly worked for me. Hopefully, it helped you too. For any left requirements, visit https://packagecontrol.io/installation#st3here.
它非常适合我。希望它也对您有所帮助。对于任何剩余要求,请在此处访问https://packagecontrol.io/installation#st3。
回答by Hymanw11111
Here is a very simple Python Sublime Text build system that works when python scripts are invoked with py file_name.py
.
这是一个非常简单的 Python Sublime Text 构建系统,当使用py file_name.py
.
Just create py.sublime-build
by Tools > Build System > New Build System
and add the contents below:
只需创建py.sublime-build
的Tools > Build System > New Build System
,并添加以下内容:
{
"cmd": ["C:\python37\python.exe", "$file"]
}
You can select it in Sublime Text editor by going to Tools > Build System > py
and building with Ctrl+ b.
您可以在 Sublime Text 编辑器中选择它,方法是Tools > Build System > py
使用Ctrl+进行构建b。
回答by Olivier Lasne
Version for Linux. Create a file ~/.config/sublime-text-3/Packages/User/Python3.sublime-build
with the following.
适用于 Linux 的版本。~/.config/sublime-text-3/Packages/User/Python3.sublime-build
使用以下内容创建文件。
{
"cmd": ["/usr/bin/python3", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"
}
回答by Tankiso Thebe
And to add on to the already solved problem, I had installed Portable Scientific Python on my flash drive E: which on another computer changed to D:, I would get the error "The system cannot find the file specified". So I used parent directory to define the path, like this:
为了补充已经解决的问题,我在我的闪存驱动器 E: 上安装了便携式科学 Python,在另一台计算机上更改为 D:,我会收到错误“系统找不到指定的文件”。所以我使用父目录来定义路径,如下所示:
From this:
由此:
<code>
{
"cmd": ["E:/WPy64-3720/python-3.7.2.amd64/python.exe", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
</code>
To this:
对此:
<code>
{
"cmd": ["../../../../WPy64-3720/python-3.7.2.amd64/python.exe","$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
</code>
You can modify depending on where your python is installed your python.
您可以根据python的安装位置进行修改。