如何从Sublime Text 2运行Python代码?

本文翻译自: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 在Win7中设置路径

  • 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. 它在下面的窗口中运行。

在此处输入图片说明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值