Geek tip: Running Python GUIs in Sublime Text 2

I’m trying out Sublime Text 2 as a code editor on Windows, since there really aren’t any other free ones I like; unfortunately, my favorite free editor, Gedit, is abysmal on Windows. (Although it looks like something may be done about that soon.) Sublime works really well and has a distraction-free mode, and the extensibility is amazing. I’m not convinced yet that it’s worth $60 for a license — I’d probably pay $25, come on, it’s a text editor — but perhaps if I got a job where I needed text editors more often, I’d try to convince my boss to spring for it. (Although in that case, I’d more likely try to convince my boss to let me work on a platform where I could run Gedit.)

One thing that confused me is that I couldn’t get any of my Python GUI code to work on it. For example, when I’m at work in my lab, I often make small changes to my laser beam profiling program (although really it’s more of a camera driver right now; I haven’t found an excuse to actually write any Gaussian profiling code.) As far as I could figure out, I ought to be able to press Ctrl+B in Sublime Text to run the program. However, nothing ever happened, and I thought the feature must be broken.

Then one time I tried running another non-GUI Python script from Sublime Text. To my surprise, it worked! The feature apparently wasn’t really broken, just selective.

The key to this mystery was the following sentence from the Sublime Text 2 reference wiki:

On Windows, GUIs are suppressed.

Wait, what the what? Why would anybody want that? I guess so the console window doesn’t show up. Fortunately, it’s easy to remedy. Go to Preferences→Browse Packages, open the Default directory and then open exec.py in the editor. Around line 26, you’ll see:

# Hide the console window on Windows
startupinfo = None
if os.name == "nt":
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

Comment out the last line of this block. Now, whenever you start a Python program, the console will be displayed. If you want to get rid of it, make your build system invoke pythonw instead of python. (Tools→Build System→New Build System, then copy Packages/Python/Python.sublime-build and change python to pythonw.)

About these ads

17 thoughts on “Geek tip: Running Python GUIs in Sublime Text 2

  1. Thanks so much ! Maybe you could give me advice for autocomplete in Sublime with Python ? Cause SublimeCodeIntel not so good.

  2. Thank you very much for this. I was getting frustrated with my programs not working in sublime. Is there any chance you could elaborate on this—”

    Comment out the last line of this block. Now, whenever you start a Python program, the console will be displayed. If you want to get rid of it, make your build system invoke pythonw instead of python. (Tools→Build System→New Build System, then copy Packages/Python/Python.sublime-build and change python to pythonw.)”

    I am unclear on what you mean by “then copy Packages/Python/Python.sublime-build and change python to pythonw.)”

    Again thank you for this. Much appreciated.

    • You’re welcome! What I meant was, create a new build system and name it something else, for instance PythonW.sublime-build. Copy the contents of the original Python.sublime-build file into PythonW.sublime-build (the copy) and change the python command to pythonw.

  3. Many thanks, really useful! I’ve been puzzling why I couldn’t get wxPython to work in Sublime Text2 – couldn’t get the display to work. Eventually, after lots of pfaffing about, found your post – thank you ;-)

  4. Honestly, I’ve tried all your steps out. ALL. (Not being angry at all, just emphasizing the word… hehehe) And they just still don’t work. :(
    I’ve commented the last line of the exec.py you said.
    Also, I’ve made a new build system, and changed python to pythonw. However, it turned out the same, I just can’t even run a “Hello, World” project.
    So, what’s plan B? Need a quick answer, mate. Thanks!
    Anyway, I’m using Windows 7. (Not looking forward to Windows 8, since I don’t have enough budget for a touchscreen laptop/convertible. Just sticking with old traditions at the moment. :D)

  5. If you followed the convention of naming your Python GUI scripts with a “.pyw” extension, you could configure ST2 to run it with pythonw.exe instead of python.exe.

      • Actually, without changing any extension settings at all, I’m able to run my Python scripts that utilize Tkinter without any problems at all. I using portable ST2 v2.0.2, build 2221 on Windows.

  6. There’s actually another way around this that doesn’t require messing around with exec.py (which will get overwritten if/when you upgrade). It also works with Sublime Text 3. Just create a new build system in the “Packages/User” directory with the following contents, and save it as Python_cmd.sublime-build:

    {
    “cmd”: [“start”, “cmd”, “/k”, “c:/pythonXX/python.exe”, “-u”, “$file”],
    “selector”: “source.python”,
    “shell”: true,
    “working_dir”: “$file_dir”
    }

    Change the “XX” to whatever version of Python you’re using, select Tools -> Build System -> Python_cmd, then hit Ctrl-B like normal to run your program. A new console will open up, and the “/k” option means it will stay open after your program is done so you can gather output, see tracebacks, etc.

    Hope this helps somebody!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s