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
.)
Thanks so much for this! 🙂 I tried messing around with the build system, but didn’t find the issue in exec.py!
Thanks so much ! Maybe you could give me advice for autocomplete in Sublime with Python ? Cause SublimeCodeIntel not so good.
No, I haven’t found anything really good yet either.
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 originalPython.sublime-build
file intoPythonW.sublime-build
(the copy) and change thepython
command topythonw
.Ah, thank you very much. Up and running.
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 😉
Do you know how to simply run Python in Sublime Text 2? I edited the Python.sublime-build file to the most recent version of Python, but I’m still getting syntax errors…
Hi Constantine, if you have a problem not related to this article then I suggest you visit the Sublime Text forum: http://www.sublimetext.com/forum/
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)
Sorry, then I don’t know – it works for me on Windows 7. Plan B is, open a command prompt and run your script manually…
Thanks a lot! Now I love sublime text even more.
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.
As far as I can tell, ST2 suppresses the GUI window on _all_ python scripts, whether they are run with python.exe or pythonw.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.
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!
Hooray!
Thank you so, so much! It freaked me out for a week, now it works fine on Windows 7, Sublime Text 3, Python 3 (gui is written in PySide).
Great solution!
Pingback: » Python:How do I run Python code from Sublime Text 2?
Thanks alot! I’ve been having a hard time with this for a while. How in the world did you figure this out? It’s not clear to me what that commented-out line even does…
I really don’t remember how I figured it out — it’s over five years ago 🙂
I’d suggest using MattDMo’s solution from the comment above anyway.
Pingback: 如何从Sublime Text 2运行Python代码?|Python问答
Pingback: 如何从Sublime Text 2运行Python代码? – 5G编程聚合网