本文翻译自:How do I run Python code from Sublime Text 2?
I want to set up a complete Python IDE in Sublime Text 2. 我想在Sublime Text 2中设置完整的Python IDE。
I want to know how to run the Python code from within the editor. 我想知道如何从编辑器中运行Python代码。 Is it done using build system? 使用构建系统完成吗? How do I do it ? 我该怎么做 ?
#1楼
参考:https://stackoom.com/question/ZshD/如何从Sublime-Text-运行Python代码
#2楼
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. 确保使用.py扩展名保存您的代码,并且该代码应该可以工作。
#3楼
您可以使用SublimeREPL (首先需要安装Package Control )。
#4楼
I solved this problem : 我解决了这个问题:
> Preferences –> Browse Packages –> Default
Open the exec.py
file, near line 41-42, the code should look like this : 打开第41-42行附近的exec.py
文件,代码应如下所示:
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'
#5楼
To RUN press Ctrl B (answer by matiit ) 要运行,请按Ctrl B (按matiit回答)
But when Ctrl B does not work , Sublime Text probably can't find the Python Interpreter. 但是,当Ctrl B 不起作用时,Sublime Text可能找不到Python解释器。 When trying to run your program, see the log and find the reference to Python in path. 尝试运行程序时,请查看日志并在路径中找到对Python的引用。
[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,cmd如下所示:
python -u C:\scripts\test.py
If you can't run python from cmd, Sublime Text can't too. 如果您无法从cmd运行python,那么Sublime Text也不会。
(Try it yourself in cmd, type python in it and run it, python commandline should appear) (在cmd中自己尝试,在其中键入python并运行它,应该显示python命令行)
SOLUTION 解
You can either change the Sublime Text build formula or the System %PATH%
. 您可以更改Sublime Text构建公式或System %PATH%
。
To set your
%PATH%
: 设置%PATH%
:
* You will need to restart your editor to load new%PATH%
* 您将需要重新启动编辑器以加载新的%PATH%
Run Command Line* and enter this command: *needs to be run as administrator 运行命令行*,然后输入以下命令: *需要以管理员身份运行
SETX /M PATH "%PATH%;<python_folder>"
for example:SETX /M PATH "%PATH%;C:\\Python27;C:\\Python27\\Scripts"
例如:SETX /M PATH "%PATH%;C:\\Python27;C:\\Python27\\Scripts"
OR manually: (preferable) 或手动:(首选)
Add;C:\\Python27;C:\\Python27\\Scripts
at the end of the string. 在字符串末尾添加;C:\\Python27;C:\\Python27\\Scripts
。
To set the interpreter's path without messing with System
%PATH%
see this answer by ppy . 要设置解释器的路径而不弄乱System%PATH%
请参阅ppy的答案。
#6楼
On Mac OS X, save your file with a .py extension. 在Mac OS X上,以.py扩展名保存文件。 Press ⌘ + B . 按⌘ + B。 It runs in a window below. 它在下面的窗口中运行。