问题:如何从Sublime Text 2运行Python代码?
我想在Sublime Text 2中设置完整的Python IDE。
我想知道如何从编辑器中运行Python代码。使用构建系统完成吗?我该怎么做 ?
I want to set up a complete Python IDE in Sublime Text 2.
I want to know how to run the Python code from within the editor. Is it done using build system? How do I do it ?
回答 0
工具->构建系统->(选择)Python然后:
跑步:
Tools -> Build
-or-
Ctrl + B
CMD + B (OSX)
这将在控制台中启动您的文件,该控制台应位于编辑器的底部。
停止:
Ctrl + Break or Tools -> Cancel Build
Fn + C (OSX)
您可以在Break
此处找到密钥的位置:http : //en.wikipedia.org/wiki/Break_key。
注意:CTRL + C
将不工作。
Ctrl + Break
不起作用时该怎么办:
去:
首选项->键绑定-用户
并粘贴以下行:
{"keys": ["ctrl+shift+c"], "command": "exec", "args": {"kill": true} }
现在,您可以使用ctrl+shift+c
代替CTRL+BREAK
Tools -> Build System -> (choose) Python then:
To Run:
Tools -> Build
-or-
Ctrl + B
CMD + B (OSX)
This would start your file in the console which should be at the bottom of the editor.
To Stop:
Ctrl + Break or Tools -> Cancel Build
Fn + C (OSX)
You can find out where your Break
key is here: http://en.wikipedia.org/wiki/Break_key.
Note: CTRL + C
will NOT work.
What to do when Ctrl + Break
does not work:
Go to:
Preferences -> Key Bindings – User
and paste the line below:
{"keys": ["ctrl+shift+c"], "command": "exec", "args": {"kill": true} }
Now, you can use ctrl+shift+c
instead of CTRL+BREAK
回答 1
在Mac OS X上,以.py扩展名保存文件。按⌘+ B。它在下面的窗口中运行。
On Mac OS X, save your file with a .py extension. Press ⌘ + B. It runs in a window below.
回答 2
编辑%APPDATA%\ Sublime Text 2 \ Python \ Python.sublime-build
将内容更改为:
{
"cmd": ["C:\\python27\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
将“ c:\ python27”部分更改为系统中具有的任何版本的python。
Edit %APPDATA%\Sublime Text 2\Python\Python.sublime-build
Change content to:
{
"cmd": ["C:\\python27\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
change the “c:\python27” part to any version of python you have in your system.
回答 3
要运行按CtrlB(通过回答matiit)
但是当CtrlB 不起作用时,Sublime Text可能找不到Python解释器。尝试运行程序时,请参阅日志并在路径中找到对Python的引用。
[cmd: [u'python', u'-u', u'C:\\scripts\\test.py']]
[path: ...;C:\Python27 32bit;...]
关键是它试图通过命令行运行python,cmd如下所示:
python -u C:\scripts\test.py
如果您无法从cmd运行python,那么Sublime Text也不会。
(在cmd中自己尝试,在其中键入python并运行它,应该显示python命令行)
解
您可以更改Sublime Text构建公式或System %PATH%
。
To RUN press CtrlB (answer by matiit)
But when CtrlB does not work, Sublime Text probably can’t find the Python Interpreter. When trying to run your program, see the log and find the reference to Python in path.
[cmd: [u'python', u'-u', u'C:\\scripts\\test.py']]
[path: ...;C:\Python27 32bit;...]
The point is that it tries to run python via command line, the cmd looks like:
python -u C:\scripts\test.py
If you can’t run python from cmd, Sublime Text can’t too.
(Try it yourself in cmd, type python in it and run it, python commandline should appear)
SOLUTION
You can either change the Sublime Text build formula or the System %PATH%
.
回答 4
回答 5
如果使用python 3.x,则需要编辑 Python3.sublime-build
(首选项>浏览包> Python 3)
看起来像这样:
{
"path": "/usr/local/bin",
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
If using python 3.x you need to edit the Python3.sublime-build
(Preferences > Browse packages > Python 3)
to look like this:
{
"path": "/usr/local/bin",
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
回答 6
[这适用于ST3(Win),不确定ST2]
要使输出在Sublime中作为另一个文件可见(为错误显示一个文件),请执行以下操作:
- 创建一个新的构建系统:
Tools > Build Systems > New Build System...
- 使用以下配置:
{
“ cmd”:[“ python.exe”,“ $ file”,“ 1>”,“ $ file_name .__ STDOUT __。txt”,“ 2>”,“ $ file_name .__ STDERR __。txt”],
“ selector”:“ source.python”,
“外壳”:是的,
“ working_dir”:“ $ file_dir”
}
- 对于您的Python文件,选择上面的构建系统配置文件:
Tools > Build Systems > {your_new_build_system_filename}
- ctrl + b
- 现在,在文件旁边,例如,
"file.py"
您将拥有"file.__STDOUT__.py"
和 "file.__STDERR__.py"
(对于错误,如果有的话) - 如果将窗口分为三列或一个网格,您将立即看到结果,而无需切换面板/窗口
[ This applies to ST3 (Win), not sure about ST2 ]
To have the output visible in Sublime as another file (+ one for errors), do this:
- Create a new build system:
Tools > Build Systems > New Build System...
- Use the following configuration:
{
"cmd": ["python.exe", "$file", "1>", "$file_name.__STDOUT__.txt", "2>", "$file_name.__STDERR__.txt"],
"selector": "source.python",
"shell": true,
"working_dir": "$file_dir"
}
- For your Python file select the above build system configuration file:
Tools > Build Systems > {your_new_build_system_filename}
- ctrl + b
- Now, next to your file, e.g.
"file.py"
you’ll have "file.__STDOUT__.py"
and "file.__STDERR__.py"
(for errors, if any) - If you split your window into 3 columns, or a grid, you’ll see the result immediately, without a need to switch panels / windows
回答 7
Cool U guys, I just found this:
http://ptomato.wordpress.com/2012/02/09/geek-tip-running-python-guis-in-sublime-text-2/
It explains (like one of the answers above) how to edit this exec.py in the default directory.
I had the problem that my PYTHON UI APPLICATION would not start. I commented out the last line from the following snipped:
# Hide the console window on Windows
startupinfo = None
if os.name == "nt":
startupinfo = subprocess.STARTUPINFO()
#startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
and, taaadaaaa, I could start my app by pressing Ctrl+B. Funny line anyways, uh? And a big thank you to whoever wrote that article ;-)
回答 8
您可以通过“查看/显示控制台”或Ctrl+ 访问Python控制台`。
You can access the Python console via “View/Show console” or Ctrl+`.
回答 9
我解决了这个问题:
> Preferences –> Browse Packages –> Default
打开第exec.py
41-42行附近的文件,代码应如下所示:
for k, v in proc_env.iteritems():
proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())
然后将其删除或编辑为:
try:
for k, v in proc_env.iteritems():
proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())
except:
print 'foobar'
I solved this problem :
> Preferences –> Browse Packages –> Default
Open the exec.py
file, near line 41-42, the code should look like this :
for k, v in proc_env.iteritems():
proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())
then delete it or edit it as :
try:
for k, v in proc_env.iteritems():
proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())
except:
print 'foobar'
回答 10
我今天遇到了同样的问题。这是我设法在Sublime Text 3中运行python代码的方式:
- 按Ctrl+ B(对于Mac,⌘+ B)开始构建系统。它应该立即执行文件。
- 遵循此答案以了解如何自定义构建系统。
接下来,您需要将内容替换Python.sublime-build
为
{
"cmd": ["/usr/local/bin/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
}
您当然可以进一步自定义它以适合您的需求。
I ran into the same problem today. And here is how I managed to run python code in Sublime Text 3:
- Press Ctrl + B (for Mac, ⌘ + B) to start build system. It should execute the file now.
- Follow this answer to understand how to customise build system.
What you need to do next is replace the content in Python.sublime-build
to
{
"cmd": ["/usr/local/bin/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
}
You can of course further customise it to something that works for you.
回答 11
在python v3.x中,您应该转到:Tools->Build System->New Build System
。
然后,在sublime文本编辑器中弹出untitled.sublime-build窗口,输入设置为:
{
"cmd": ["path_to_the_python.exe","-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
要查看路径,请执行以下操作Type following in terminal as
:
python
>>> import sys
>>>print(sys.executable)
您可以制作多个构建系统,但默认情况下,应将其保存在具有.sublime-build扩展名的Sublime文本包中。
然后,选择新的构建系统 ,然后cltr+b
根据您的操作系统按或其他。
In python v3.x you should go to : Tools->Build System->New Build System
.
Then, it pop up the untitled.sublime-build window in sublime text editor.Enter setting as:
{
"cmd": ["path_to_the_python.exe","-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
To see the path, Type following in terminal as
:
python
>>> import sys
>>>print(sys.executable)
You can make more than one Build System but it should default save inside Packages of Sublime text with .sublime-build extension.
Then, select the new Build System and press cltr+b
or other based on your os.
回答 12
我有同样的问题。您可能尚未保存文件。确保使用.py扩展名保存您的代码,并且该代码应该可以工作。
I had the same problem. You probably haven’t saved the file yet. Make sure to save your code with .py extension and it should work.
回答 13
关于上述构建系统要注意的一件事:您可以编写(并使用)自定义.sublime-build
文件,甚至可以编写每个项目build_systems
子句(在项目设置中)。这使您可以做一些有用的事情,例如使用ANSI颜色输出的精美测试运行程序。
要获得更多的“完整IDE”功能,可以使用出色的SublimePythonIDE软件包:
- 代码完成(英特尔)
- 跳转到定义和对象描述
- 适当的棉绒/ pep8
- 用virtualenv支持不同的解释器
披露:我已经贡献了PR该程序包,我用它所有的时间,但也 有 其他人。
One thing to note about the aforementioned build system: you can write (and use) custom .sublime-build
files or even per project build_systems
clause (in your project settings). This allows you to do useful things like a fancy test runner with ANSI colors output.
For even more “full IDE” features, you can use the excellent SublimePythonIDE package:
- code completion (intel)
- jump to definition & object description
- proper linting/pep8
- supports different interpreters with virtualenv
Disclosure: I’ve contributed a PR to that package, and I use it all the time, but there are others.
回答 14
在Sublime旁边使用真正的python控制台
Sublime的构建系统和SublimeREPL(上面的答案)都受到限制,因为您在运行文件后无法轻松地与工作空间变量进行交互。
如果要运行脚本,然后以类似于REPL的方式工作(就像在IDE中一样),则建议将Sublime与IPython控制台一起打开。您可以使用AutoHotKey(Windows)或AutoKey(Linux)进行设置,以使单个快捷方式将复制文件名(或仅复制所选的代码),然后将其粘贴到控制台中以运行文件。
Linux或Windows的详细说明
Use a real python console alongside Sublime
Both Sublime’s build system and SublimeREPL (the answers above) are limited in that you can’t easily interact with the workspace variables after you run your file.
If you want to run a script, then work in a REPL-like fashion (like you would in an IDE), then I recommend having Sublime open alongside an IPython console. Using AutoHotKey (Windows) or AutoKey (Linux), you can set this up such that a single shortcut will copy the filename (or just the selected code) and then paste this in the console to run the file.
Detailed instructions for Linux or Windows
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。