如何从卓越的文本2中运行Python代码?

时间:2022-03-21 07:12:16

I want to set up a complete Python IDE in Sublime Text 2.

我想在崇高的文本2中建立一个完整的Python IDE。

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 ?

我想知道如何从编辑器中运行Python代码。它是使用构建系统完成的吗?我该怎么做?

15 个解决方案

#1


336  

Tools -> Build System -> (choose) Python then:

工具->构建系统->(选择)Python:

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.

你可以在这里找到你的破解钥匙:http://en.wikipedia.org/wiki/Break_key。

Note: CTRL + C will NOT work.

注意:CTRL + C不能工作。

What to do when Ctrl + Break does not work:

当Ctrl + Break不工作时该怎么办:

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

现在可以使用ctrl+shift+c而不是ctrl+ BREAK。

#2


54  

Edit %APPDATA%\Sublime Text 2\Python\Python.sublime-build

Python编辑% APPDATA % \崇高文本2 \ \ 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.

将“c:\python27”部分更改为系统中任何版本的python。

#3


50  

On Mac OS X, save your file with a .py extension. Press + B. It runs in a window below.

在Mac OS X上,用.py扩展名保存文件。按⌘+ b,它运行在一个窗口。

如何从卓越的文本2中运行Python代码?

#4


43  

To RUN press CtrlB (answer by matiit)

按下按ctrl键(由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.

但是当ctrl lb不工作时,崇高的文本可能找不到Python解释器。在尝试运行您的程序时,请参见日志,并在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.
(Try it yourself in cmd, type python in it and run it, python commandline should appear)

如果您不能从cmd运行python,那么崇高的文本也不能。(在cmd中尝试一下,在它里面输入python,然后运行它,python命令行应该出现)

SOLUTION

You can either change the Sublime Text build formula or the System %PATH%.

您可以更改卓越的文本构建公式或系统%PATH%。

  • To set your %PATH%:
    *You will need to restart your editor to load new %PATH%

    要设置%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%; ”,例如:SETX /M路径“%PATH%;C:\Python27;C:\Python27\Scripts”

    • OR manually: (preferable)
      Add ;C:\Python27;C:\Python27\Scripts at the end of the string. 如何从卓越的文本2中运行Python代码?

      或者手动:(更可取)添加;C:\Python27;C:\Python27\脚本在字符串的末尾。

  • To set the interpreter's path without messing with System %PATH% see this answer by ppy.

    要设置解释器的路径,而不影响系统% path %,请使用ppy来查看这个答案。

#5


9  

You can use SublimeREPL (you need to have Package Control installed first).

您可以使用SublimeREPL(首先需要安装包控制)。

#6


7  

If using python 3.x you need to edit the Python3.sublime-build

如果使用python 3。您需要编辑Python3.sublime-build。

(Preferences > Browse packages > Python 3)

(Preferences >浏览包> Python 3)

to look like this:

看起来像这样:

{
  "path": "/usr/local/bin",
  "cmd": ["python3", "-u", "$file"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.python"
}

#7


4  

[ This applies to ST3 (Win), not sure about ST2 ]

[这适用于ST3 (Win),不确定ST2]

To have the output visible in Sublime as another file (+ one for errors), do this:

要使输出显示为另一个文件(+ 1错误),请执行以下操作:

  1. Create a new build system: Tools > Build Systems > New Build System...
  2. 创建一个新的构建系统:工具>构建系统>新构建系统…
  3. Use the following configuration:
  4. 使用以下配置:

    {
        "cmd": ["python.exe", "$file", "1>", "$file_name.__STDOUT__.txt", "2>", "$file_name.__STDERR__.txt"],
        "selector": "source.python",
        "shell": true,
        "working_dir": "$file_dir"
    }
  1. For your Python file select the above build system configuration file: Tools > Build Systems > {your_new_build_system_filename}
  2. 对于您的Python文件选择上述构建系统配置文件:工具>构建系统> {your_new_build_system_filename}
  3. ctrl + b
  4. ctrl + b
  5. Now, next to your file, e.g. "file.py" you'll have "file.__STDOUT__.py" and "file.__STDERR__.py" (for errors, if any)
  6. 现在,在你的文件旁边。”文件。py file.__STDOUT__“你”。py”和“file.__STDERR__。py“(如果有错误的话)
  7. If you split your window into 3 columns, or a grid, you'll see the result immediately, without a need to switch panels / windows
  8. 如果您将窗口分割为3个列,或者一个网格,您将立即看到结果,而不需要切换面板/窗口。

#8


2  

Cool U guys, I just found this:

酷伙计们,我刚刚发现

http://ptomato.wordpress.com/2012/02/09/geek-tip-running-python-guis-in-sublime-text-2/

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.

它解释了(就像上面的一个答案)如何编辑这个exec。默认目录下的py。

I had the problem that my PYTHON UI APPLICATION would not start. I commented out the last line from the following snipped:

我遇到了我的PYTHON UI应用程序无法启动的问题。我把最后一行字注释掉了:

    # 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 ;-)

taaadaaaa,我可以按Ctrl+B来启动app。无论如何,玩笑话啊?对写这篇文章的人表示感谢;-)

#9


1  

You can access the Python console via “View/Show console” or Ctrl+`.

您可以通过“视图/显示控制台”或Ctrl+来访问Python控制台。

#10


1  

I solved this problem :

我解决了这个问题:

> Preferences –> Browse Packages –> Default 

Open the exec.py file, near line 41-42, the code should look like this :

打开执行。py文件,在第41-42行附近,代码应该是这样的:

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'

#11


1  

I ran into the same problem today. And here is how I managed to run python code in Sublime Text 3:

我今天遇到了同样的问题。下面是我如何在崇高的文本3中运行python代码:

  1. Press Ctrl + B (for Mac, + B) to start build system. It should execute the file now.
  2. 按Ctrl + B (Mac, + B)开始构建系统。它现在应该执行文件。
  3. Follow this answer to understand how to customise build system.
  4. 按照这个答案来理解如何定制构建系统。

What you need to do next is replace the content in Python.sublime-build to

接下来需要做的是用Python替换内容。sublime-build来

{
    "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.

当然,你可以进一步定制适合你的东西。

#12


0  

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扩展,它应该可以工作。

#13


0  

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.

关于上述构建系统需要注意的一点是:您可以编写(和使用)自定义.sublime-build文件,甚至每个项目build_system子句(在您的项目设置中)。这让你可以做一些有用的事情,比如一个有ANSI颜色输出的漂亮的测试跑步者。

For even more "full IDE" features, you can use the excellent SublimePythonIDE package:

对于更“完整的IDE”特性,您可以使用优秀的SublimePythonIDE包:

  • code completion (intel)
  • 代码自动完成(英特尔)
  • jump to definition & object description
  • 跳转到定义和对象描述。
  • proper linting/pep8
  • 适当的产品毛羽/ pep8
  • supports different interpreters with virtualenv
  • 支持不同的解释器与virtualenv。

Disclosure: I've contributed a PR to that package, and I use it all the time, but there are others.

披露:我已经为那个包做了一个公关,我一直在使用它,但是还有其他的。

#14


0  

In python v3.x you should go to : Tools->Build System->New Build System.

在python中v3。你应该去:工具->构建系统->新的构建系统。

Then, it pop up the untitled.sublime-build window in sublime text editor.Enter setting as:

然后,它突然冒出来。在卓越的文本编辑器中创建一个“sublime-build”窗口。进入设置为:

{

    "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.

您可以创建一个以上的构建系统,但它应该默认保存在带有.sublime- Build扩展的崇高文本包内。

Then, select the new Build System and press cltr+b or other based on your os.

然后,选择新的构建系统,并根据您的操作系统按下cltr+b或其他。

#15


0  

Use a real python console alongside Sublime

使用一个真正的python控制台和崇高。

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.

崇高的构建系统和SublimeREPL(上面的答案)都是有限的,因为在运行完文件之后,您无法轻松地与工作空间变量进行交互。

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.

如果您想要运行一个脚本,然后以一个类似于replyview的方式工作(就像您在IDE中那样),那么我建议您在IPython控制台旁边有一个卓越的开放。使用AutoHotKey (Windows)或AutoKey (Linux),您可以将其设置为单个快捷方式将复制filename(或者只是选定的代码),然后将其粘贴到控制台以运行该文件。

Detailed instructions for Linux or Windows

Linux或Windows的详细说明。

#1


336  

Tools -> Build System -> (choose) Python then:

工具->构建系统->(选择)Python:

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.

你可以在这里找到你的破解钥匙:http://en.wikipedia.org/wiki/Break_key。

Note: CTRL + C will NOT work.

注意:CTRL + C不能工作。

What to do when Ctrl + Break does not work:

当Ctrl + Break不工作时该怎么办:

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

现在可以使用ctrl+shift+c而不是ctrl+ BREAK。

#2


54  

Edit %APPDATA%\Sublime Text 2\Python\Python.sublime-build

Python编辑% APPDATA % \崇高文本2 \ \ 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.

将“c:\python27”部分更改为系统中任何版本的python。

#3


50  

On Mac OS X, save your file with a .py extension. Press + B. It runs in a window below.

在Mac OS X上,用.py扩展名保存文件。按⌘+ b,它运行在一个窗口。

如何从卓越的文本2中运行Python代码?

#4


43  

To RUN press CtrlB (answer by matiit)

按下按ctrl键(由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.

但是当ctrl lb不工作时,崇高的文本可能找不到Python解释器。在尝试运行您的程序时,请参见日志,并在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.
(Try it yourself in cmd, type python in it and run it, python commandline should appear)

如果您不能从cmd运行python,那么崇高的文本也不能。(在cmd中尝试一下,在它里面输入python,然后运行它,python命令行应该出现)

SOLUTION

You can either change the Sublime Text build formula or the System %PATH%.

您可以更改卓越的文本构建公式或系统%PATH%。

  • To set your %PATH%:
    *You will need to restart your editor to load new %PATH%

    要设置%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%; ”,例如:SETX /M路径“%PATH%;C:\Python27;C:\Python27\Scripts”

    • OR manually: (preferable)
      Add ;C:\Python27;C:\Python27\Scripts at the end of the string. 如何从卓越的文本2中运行Python代码?

      或者手动:(更可取)添加;C:\Python27;C:\Python27\脚本在字符串的末尾。

  • To set the interpreter's path without messing with System %PATH% see this answer by ppy.

    要设置解释器的路径,而不影响系统% path %,请使用ppy来查看这个答案。

#5


9  

You can use SublimeREPL (you need to have Package Control installed first).

您可以使用SublimeREPL(首先需要安装包控制)。

#6


7  

If using python 3.x you need to edit the Python3.sublime-build

如果使用python 3。您需要编辑Python3.sublime-build。

(Preferences > Browse packages > Python 3)

(Preferences >浏览包> Python 3)

to look like this:

看起来像这样:

{
  "path": "/usr/local/bin",
  "cmd": ["python3", "-u", "$file"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.python"
}

#7


4  

[ This applies to ST3 (Win), not sure about ST2 ]

[这适用于ST3 (Win),不确定ST2]

To have the output visible in Sublime as another file (+ one for errors), do this:

要使输出显示为另一个文件(+ 1错误),请执行以下操作:

  1. Create a new build system: Tools > Build Systems > New Build System...
  2. 创建一个新的构建系统:工具>构建系统>新构建系统…
  3. Use the following configuration:
  4. 使用以下配置:

    {
        "cmd": ["python.exe", "$file", "1>", "$file_name.__STDOUT__.txt", "2>", "$file_name.__STDERR__.txt"],
        "selector": "source.python",
        "shell": true,
        "working_dir": "$file_dir"
    }
  1. For your Python file select the above build system configuration file: Tools > Build Systems > {your_new_build_system_filename}
  2. 对于您的Python文件选择上述构建系统配置文件:工具>构建系统> {your_new_build_system_filename}
  3. ctrl + b
  4. ctrl + b
  5. Now, next to your file, e.g. "file.py" you'll have "file.__STDOUT__.py" and "file.__STDERR__.py" (for errors, if any)
  6. 现在,在你的文件旁边。”文件。py file.__STDOUT__“你”。py”和“file.__STDERR__。py“(如果有错误的话)
  7. If you split your window into 3 columns, or a grid, you'll see the result immediately, without a need to switch panels / windows
  8. 如果您将窗口分割为3个列,或者一个网格,您将立即看到结果,而不需要切换面板/窗口。

#8


2  

Cool U guys, I just found this:

酷伙计们,我刚刚发现

http://ptomato.wordpress.com/2012/02/09/geek-tip-running-python-guis-in-sublime-text-2/

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.

它解释了(就像上面的一个答案)如何编辑这个exec。默认目录下的py。

I had the problem that my PYTHON UI APPLICATION would not start. I commented out the last line from the following snipped:

我遇到了我的PYTHON UI应用程序无法启动的问题。我把最后一行字注释掉了:

    # 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 ;-)

taaadaaaa,我可以按Ctrl+B来启动app。无论如何,玩笑话啊?对写这篇文章的人表示感谢;-)

#9


1  

You can access the Python console via “View/Show console” or Ctrl+`.

您可以通过“视图/显示控制台”或Ctrl+来访问Python控制台。

#10


1  

I solved this problem :

我解决了这个问题:

> Preferences –> Browse Packages –> Default 

Open the exec.py file, near line 41-42, the code should look like this :

打开执行。py文件,在第41-42行附近,代码应该是这样的:

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'

#11


1  

I ran into the same problem today. And here is how I managed to run python code in Sublime Text 3:

我今天遇到了同样的问题。下面是我如何在崇高的文本3中运行python代码:

  1. Press Ctrl + B (for Mac, + B) to start build system. It should execute the file now.
  2. 按Ctrl + B (Mac, + B)开始构建系统。它现在应该执行文件。
  3. Follow this answer to understand how to customise build system.
  4. 按照这个答案来理解如何定制构建系统。

What you need to do next is replace the content in Python.sublime-build to

接下来需要做的是用Python替换内容。sublime-build来

{
    "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.

当然,你可以进一步定制适合你的东西。

#12


0  

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扩展,它应该可以工作。

#13


0  

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.

关于上述构建系统需要注意的一点是:您可以编写(和使用)自定义.sublime-build文件,甚至每个项目build_system子句(在您的项目设置中)。这让你可以做一些有用的事情,比如一个有ANSI颜色输出的漂亮的测试跑步者。

For even more "full IDE" features, you can use the excellent SublimePythonIDE package:

对于更“完整的IDE”特性,您可以使用优秀的SublimePythonIDE包:

  • code completion (intel)
  • 代码自动完成(英特尔)
  • jump to definition & object description
  • 跳转到定义和对象描述。
  • proper linting/pep8
  • 适当的产品毛羽/ pep8
  • supports different interpreters with virtualenv
  • 支持不同的解释器与virtualenv。

Disclosure: I've contributed a PR to that package, and I use it all the time, but there are others.

披露:我已经为那个包做了一个公关,我一直在使用它,但是还有其他的。

#14


0  

In python v3.x you should go to : Tools->Build System->New Build System.

在python中v3。你应该去:工具->构建系统->新的构建系统。

Then, it pop up the untitled.sublime-build window in sublime text editor.Enter setting as:

然后,它突然冒出来。在卓越的文本编辑器中创建一个“sublime-build”窗口。进入设置为:

{

    "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.

您可以创建一个以上的构建系统,但它应该默认保存在带有.sublime- Build扩展的崇高文本包内。

Then, select the new Build System and press cltr+b or other based on your os.

然后,选择新的构建系统,并根据您的操作系统按下cltr+b或其他。

#15


0  

Use a real python console alongside Sublime

使用一个真正的python控制台和崇高。

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.

崇高的构建系统和SublimeREPL(上面的答案)都是有限的,因为在运行完文件之后,您无法轻松地与工作空间变量进行交互。

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.

如果您想要运行一个脚本,然后以一个类似于replyview的方式工作(就像您在IDE中那样),那么我建议您在IPython控制台旁边有一个卓越的开放。使用AutoHotKey (Windows)或AutoKey (Linux),您可以将其设置为单个快捷方式将复制filename(或者只是选定的代码),然后将其粘贴到控制台以运行该文件。

Detailed instructions for Linux or Windows

Linux或Windows的详细说明。