在 Mac 上使用 Python 3.5.0 + Sublime 3.0 运行代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33205895/
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
Running Code with Python 3.5.0 + Sublime 3.0 on Mac
提问by Chris's Dad
My question is, can Sublime 3 be setup to run the code that has been written with in Sublime. I've have searched on here and the internet and have tried numerous different suggestions. If the answer has already been posted and someone could point me in the proper direction / URL I'd appreciate it. If it cannot be done and you have other suggestion's I'll give it a try.
我的问题是,能否将 Sublime 3 设置为运行在 Sublime 中编写的代码。我已经在这里和互联网上进行了搜索,并尝试了许多不同的建议。如果答案已经发布并且有人可以为我指出正确的方向/网址,我将不胜感激。如果无法完成并且您有其他建议,我会尝试一下。
回答by shanmuga
Yes sublime can execute code in python3.
Sublime text executes the python file using system python available.
All you need to do is make python3 is default in your system. You can do this by adding setting PATH
variable to point to python3.
是的 sublime 可以在 python3 中执行代码。Sublime text 使用可用的系统 python 执行 python 文件。
您需要做的就是让 python3 在您的系统中成为默认值。您可以通过添加设置PATH
变量以指向 python3来做到这一点。
To execute code:
执行代码:
Tools -> Build
CMD + B
回答by HelloWorld
The following steps do work for Sublime Text 2and 3. What you need is a so-called Sublime Text Build Systemwhich is represented by a valid JSON text file. There are a lot of Q and A to this topic on the internet. Anyway, here is a step-by-step list.
以下步骤适用于 Sublime Text 2和3。你需要的是一个所谓的 Sublime Text Build System,它由一个有效的 JSON 文本文件表示。互联网上有很多关于这个话题的问答。无论如何,这是一个分步列表。
FYI: As far as I know there is no updated Syntax file for Python3.5 for Sublime Text. If anyone knows of an update, please let me know in the comment section.
仅供参考:据我所知,Sublime Text 的 Python3.5 没有更新的语法文件。如果有人知道更新,请在评论部分告诉我。
Mac and Linux:
Mac 和 Linux:
- Open Sublime Text
- In the menu bar go to
Tools -> Build-System -> New Build System
Paste the following code-snippet to the new opened file. Verify that the path to your python 3.5 installation is correct, otherwise see step 4.
{ "cmd": ["/Library/Frameworks/Python.framework/Versions/3.5/bin/python3", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }
If you don't know the location of Python 3 try to execute 'which python3' in your terminal. Also verify that the correct python3 command is in your search-path. Here is some example output:
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
Save the file using (e.g using
cmd (?) + s
on your keyboard) and enter a filename likePython-3.5.sublime-build
.Now you can switch to your new build-system. By using (
cmd + b
) you can execute your Python script now.
- 打开 Sublime 文本
- 在菜单栏中转到
Tools -> Build-System -> New Build System
将以下代码片段粘贴到新打开的文件中。验证您的 python 3.5 安装路径是否正确,否则请参阅步骤 4。
{ "cmd": ["/Library/Frameworks/Python.framework/Versions/3.5/bin/python3", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }
如果您不知道 Python 3 的位置,请尝试在终端中执行“which python3”。还要验证正确的 python3 命令是否在您的搜索路径中。这是一些示例输出:
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
使用(例如
cmd (?) + s
在键盘上使用)保存文件并输入文件名,如Python-3.5.sublime-build
.现在您可以切换到新的构建系统。通过使用 (
cmd + b
),您现在可以执行 Python 脚本。
Windows:
视窗:
- Open Sublime Text
- In the menu bar go to
Tools -> Build-System -> New Build System
Paste the following code-snippet to the new opened file. You need to verify that the path to
python.exe
is correct, otherwise update it.{ "cmd": [r"C:\Python35\python.exe", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }
Save the file using (e.g using
CTRL + s
on your keyboard) and enter a filename likePython-3.5.sublime-build
.Now you can switch to your new build-system. By using (
CTRL + b
) you can execute your Python script now.
- 打开 Sublime 文本
- 在菜单栏中转到
Tools -> Build-System -> New Build System
将以下代码片段粘贴到新打开的文件中。您需要验证路径
python.exe
是否正确,否则更新它。{ "cmd": [r"C:\Python35\python.exe", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }
使用(例如
CTRL + s
在键盘上使用)保存文件并输入文件名,如Python-3.5.sublime-build
.现在您可以切换到新的构建系统。通过使用 (
CTRL + b
),您现在可以执行 Python 脚本。
回答by Douzi
By using the following code:
通过使用以下代码:
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
would make the Sublime work.
将使 Sublime 工作。
Save the file as python3.sublime-build
then select it in Tools -> build system -> Python3
.
将文件另存为,python3.sublime-build
然后在Tools -> build system -> Python3
.
At last, run it by pressing command+ B.
最后,按command+运行它B。
回答by valex
For python 3.6
i'm using:
因为python 3.6
我正在使用:
{
"cmd": ["/usr/local/bin/python3.6", "$file"]
}
And everything works fine! Don't forget to get full path to your python interpreter.
一切正常!不要忘记获取 python 解释器的完整路径。