How do I execute a Python code

How do I run Sublime Text 2 python code?


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

I want to know how the python code is executed in the editor. Is it done with the build system? How do I do it ?

Reply:


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

To run:

This would launch your file in the console, which should be at the bottom of the editor.

To stop:

You can find out where your key is here: http://en.wikipedia.org/wiki/Break_key.

Note: will Not function.

What to do if it doesn't work

Go to:

Settings -> Keyboard Shortcuts - User

and insert the following line:

Now you can use instead of







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


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

Change the content to:

Change the "c: \ python27" part to whatever version of Python you have on your system.






To run press (answer by Matiit)

But if this doesn't work , Sublime Text probably cannot find the Python interpreter. Whenever you try to run your program, read the log and look for the reference to Python in the path.

The point is that it tries to run python from the command line, the cmd looks like:

If you can't run Python from cmd, neither can Sublime Text.
(Try it yourself in cmd, type Python and run it. The Python command line should appear.)

You can either change the build formula for Sublime Text or the system.

  • How to set:
    * You will need to restart your editor to load new ones

    • Run the command line * and enter the following command: * must be run as an administrator,

      for example:

    • OR manual: (preferred)
      add at the end of the string.

  • To set the interpreter's path without messing around with the system, check out this answer from ppy.





If you're using Python 3.x you'll need to edit that

(Settings> Search Packages> Python 3)

look like this:


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

To make the output appear as a different file in Sublime (+ one for errors), do the following:

  1. Create a new build system:
  2. 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"}}
  1. For your Python file, select the above build system configuration file:
  2. +
  3. Well, besides your file, e.g. have and (for errors, if any)
  4. If you split your window into 3 columns or a grid you will see the result immediately without having to switch between panels / windows

Cool U folks, I just found this:

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

Here Experienced You (as in one of the answers above) like this exec.py in the Default directory to edit .

I had the problem that mine PYTHON UI APPLICATION could not be started. I commented out the last line from the following excerpt:

and, taaadaaaa, I could start my app by pressing Ctrl + B. Funny line anyway, isn't it? And a big one Thank-you to everyone who wrote this article ;-)



You can access the Python console from "Show / Show Console" or +.






I solved this problem:

Open the file near line 41-42. The code should look like this:

then delete or edit as:





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

  1. Press + (for Mac, +) to start the build system. It should now run the file.
  2. Follow this answer to understand how to customize the build system.

What you need to do next is replace the content in too

You can of course further customize it to something that works for you.


In Python v3.x, you should go to:.

The untitled.sublime-build window is then opened in the Sublime text editor. Enter the setting as follows:

To see the path:

You can have more than one build system create , however, it should be in Packages with raised text With the extension .sublime-build .

Then choose the new one Build system off and press or others based on your operating system.


I had the same problem. You probably haven't saved the file yet. Make sure to save your code with a .py extension and it should work.


One thing to keep in mind with the above build system: you can write (and use) custom files or even per project clause (in your project settings). That way, you can do useful things like a fancy test runner with ANSI color output.

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

  • Code completion (Intel)
  • Jump to definition & object description
  • real fluff / pep8
  • supports various interpreters with virtualenv

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


Use a real Python console in addition to Sublime

Both Sublime's build system and SublimeREPL (the answers above) are limited in that you cannot simply interact with the workspace variables after you run your file.

If you're running a script and then want to work REPL-like (like in an IDE), I recommend opening Sublime next to an IPython console. With AutoHotKey (Windows) or AutoKey (Linux) you can set this up so that a single shortcut copies the file name (or just the selected code) and then pastes it into the console to run the file.

Detailed instructions for Linux or Windows

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.