64

I want to configure Sublime Text 3 to build Python 3, but I don't seem to understand how the builds work. Many tutorials have told me to make a build file containing code such as:

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

and save it as a file called Python.sublime-build or python3.sublime-build (much of the information I found was conflicting). One tutorial suggested creating a new folder in the ST3 Packages folder called Python and add the build file in there, whilst other tutorials suggested leaving it in the folder called User.

One tutorial explained how I had to change the Environment Variable path on my operating system to get it to work. That didn't seem to help either.


I added a folder Python to Packages (since it wasn't there already) and added in a build file with the name Python.sublime_build which featured only the code I posted above in it. Now when I attempt to run Sublime Text it gives me this error:

Error trying to parse build system:
Expected value in Packages\Python\Python.sublime-build:2:5
6
  • 1
    What do you mean by "get sublime text 3 to work with python 3"? I use Sublime 3, downloaded from sublimetext.com/3, and it works with python 3 out of the box...
    – Moritz
    Commented May 19, 2014 at 6:49
  • Could it be due to the fact that I downloaded the wrong version (x32 when I really needed x64)? Because we downloaded from the same location and I can't get python 3 to work at all. I run a 64-bit Operating System, but I assumed that wouldn't make a difference since many of my other programs are also x32. Commented May 19, 2014 at 6:52
  • 1
    Panicking about a downvote isn't going to get this fixed. Explain what you want done (clearly), answer @Moritz's question and explain what you've done thus far (because you've done something, but I'm not sure what). I also suggest removing all of the non-relevant information (I'll give an edit for that).
    – Veedrac
    Commented May 19, 2014 at 6:54
  • 2
    Alright, my apologies, I just overreacted since I've received multiple unexplained downvotes on questions I thought were clear tonight. @Moritz, every time I try to build a program with Sublime Text, it builds it using Python 2.7.3, when the build file I created specifies that it should be using Python 3.2.5 (the currently installed version of python 3 on my computer). Commented May 19, 2014 at 6:58
  • 1
    What OS, and is that the full error? (I can't help more as I've got to go.)
    – Veedrac
    Commented May 19, 2014 at 7:08

10 Answers 10

91

The reason you're getting the error is that you have a Unix-style path to the python executable, when you're running Windows. Change /usr/bin/python3 to C:/Python32/python.exe (make sure you use the forward slashes / and not Windows-style back slashes \). Once you make this change, you should be all set.

Also, you need to change the single quotes ' to double quotes " like so:

{
    "cmd": ["c:/Python32/python.exe", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

The .sublime-build file needs to be valid JSON, which requires strings be wrapped in double quotes, not single.

3
  • Thank you so much! This was causing me so much trouble. My apologies again for freaking out before, that was immature. Commented May 19, 2014 at 15:29
  • there's one more difference in the question vs this answer (which solved my problem+thanks); in the question, "..." is a character all on it's own, instead of 3 . characters.
    – emmagras
    Commented Feb 7, 2015 at 21:46
  • 1
    @shuboy2014 As I stated in my answer, you need to use forward slashes as path delimiters. After changing that, there's no reason why your path wouldn't work, as long as that is where Python is installed.
    – MattDMo
    Commented Oct 21, 2016 at 17:26
20

Steps to Make Sublime Text a Python IDE (Windows)

Tested successfully on Sublime Text 3. Assuming Sublime Text and package control are already installed . . .

  1. Install Python (python.org) and pay attention to where it is installed or choose a simple location like the C drive, agreeing to remove character limit at the end of the installation.

  2. Install package SublimeREPL (Cntrl + Shift + P, Package Control - Install Package, SublimeREPL, Enter).

  3. Go to Preferences, Package Settings, SublimeREPL, Settings - User.

  4. Paste in the following, updating the file path to your python installation folder, as needed. You may customize these and choose whatever syntax you like (last line) but I prefer my output in plain text.

    {
      "default_extend_env": {"PATH":"C:\\Program Files\\Python36\\"},
      "repl_view_settings": {
      "translate_tabs_to_spaces": false,
      "auto_indent": false,
      "smart_indent": false,
      "spell_check": false,
      "indent_subsequent_lines": false,
      "detect_indentation": false,
      "auto_complete": true,
      "line_numbers": false,
      "gutter": false,
      "syntax": "Packages/Text/Plain text.tmLanguage"
      }
    }
    
  5. Save and close the file (SublimeREPL.sublime-settings).

  6. Go to Tools, Build System, New Build System.

  7. Replace all existing text with the following:

    {
    "target": "run_existing_window_command", 
    "id": "repl_python_run",
    "file": "config/Python/Main.sublime-menu"
    }
    
  8. Cntrl + S or save as "C:\Users[username]\AppData\Roaming\Sublime Text 3\Packages\User\SublimeREPL-python.sublime-build" updating username or path as needed. This should be wherever your settings and builds are stored by Sublime Text.

  9. Go to Tools, Build System, select SublimeREPL-python.

  10. All done--now to test. Open or create a simple python file, having a *.py extension and save it wherever desired.

  11. Make sure the file is open and selected in Sublime Text. Now, when you press Cntrl + B to build and run it, it will open another tab, titled "REPL [python]", executing and displaying the results of your python code.

If you would like to go a step further, I highly recommend making the follow changes, to allow Sublime to reload your executed python in the same window, when you press Cntrl+B (Build), instead of it opening a new tab each time:

Add the following line in the "repl_python_run" command in (Preferences, Browse Packages) SublimeREPL\config\Python\Main.sublime-menu, right before the "external_id": "python" argument:

"view_id": "*REPL* [python]",

and then to change the line:

if view.id() == view_id

into:

if view.name() == view_id

in SublimeREPL\sublimerepl.py.

4
  • This is not the answer that we deserve..... but the answer that we needed. Thumbs Up!
    – Alfa Bravo
    Commented Nov 30, 2017 at 21:16
  • Do you have any idea of why when I build, it does not save before it builds (even if I have checked the check 'save on build' on version 3.2?) Commented Apr 15, 2019 at 16:09
  • @Giovanni G. PY - Sorry, I haven't been able to reproduce that. I'm running Sublime Text 3.2.2, Build 3211 and can verify that it saves the source file when I press Cntrl + Shift + B while cursor is in said file, as it executes the build and displays the REPL window.
    – Daniel
    Commented Mar 8, 2020 at 0:29
  • @Daniel Now it works, I don't know how I made it, if it was for an update or for something I changed though Commented Mar 8, 2020 at 5:40
7

If you are using PyQt, then for normal work, you should add "shell":"true" value, this looks like:

{
  "cmd": ["c:/Python32/python.exe", "-u", "$file"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.python",
  "shell":"true"
}
1
  • 1
    Thank you ! By adding "shell":"true", I got rid of the warning message about UNC paths on windows (UNC paths are not supported. Defaulting to Windows directory)
    – YYY
    Commented Mar 15, 2021 at 16:18
5

Run Python Files in Sublime Text3

For Sublime Text 3, First Install Package Control:

  • Press Ctrl + Shift + P, a search bar will open
  • Type Install package and then press enter Click here to see Install Package Search Pic

  • After the package got installed. It may prompt to restart SublimeText

  • After completing the above step
  • Just again repeat the 1st and 2nd step, it will open the repositories this time
  • Search for Python 3 and Hit enter.
  • There you go.
  • Just press Ctrl + B in your python file and you'll get the output. Click here to see Python 3 repo pic

It perfectly worked for me. Hopefully, it helped you too. For any left requirements, visit https://packagecontrol.io/installation#st3 here.

3

Steps for configuring Sublime Text Editor3 for Python3 :-

  1. Go to preferences in the toolbar.
  2. Select Package Control.
  3. A pop up will open.
  4. Type/Select Package Control:Install Package.
  5. Wait for a minute till repositories are loading.
  6. Another Pop up will open.
  7. Search for Python 3.
  8. Now sublime text is set for Python3.
  9. Now go to Tools-> Build System.
  10. Select Python3.

Enjoy Coding.

1
  • i found this help simple and in easy steps. Commented Jan 10, 2021 at 5:31
1

Version for Linux. Create a file ~/.config/sublime-text-3/Packages/User/Python3.sublime-build with the following.

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

And to add on to the already solved problem, I had installed Portable Scientific Python on my flash drive E: which on another computer changed to D:, I would get the error "The system cannot find the file specified". So I used parent directory to define the path, like this:

From this:

{
    "cmd": ["E:/WPy64-3720/python-3.7.2.amd64/python.exe", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

To this:

{
    "cmd": ["../../../../WPy64-3720/python-3.7.2.amd64/python.exe","$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

You can modify depending on where your python is installed your python.

0

I'd like to add just one point to the accepted answer:

when you edit the cmd portion of the snippet below, make sure to add the file address (with forward slash) where python is kept on your computer.

{
"cmd": ["c:/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

How to know where python is installed on your system? Two ways for windows users:

  1. Open Command prompt and do the following query: where python If it shows the path to python.exe, copy and paste it with /. If it shows some error, then follow process below:
  2. Go to start -> Search for python shortcut (not IDLE) -> Right Click -> More -> Open file location -> Right click the shortcut -> Properties -> Target -> Path is now visible -> Copy and paste it in the field above.
0

first you need to find your python.exe location, to find location run this python script:

import sys
print(sys.executable)

Then you can create your custom python build:

{
"cmd": ["C:\\Users\\*username*\\AppData\\Local\\Programs\\Python\\Python39\\python.exe", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"}

You can change the location, In my case it is C:\Users*username*\AppData\Local\Programs\Python\Python39\python.exe

Then save your new build. Don't change the file extension while saving.

2
  • You should have not posted the path as it is on the forum. It exposes your system file address. This is a personal data, and may be misued.
    – lousycoder
    Commented May 22, 2021 at 9:20
  • @lousycoder yep . I changed it Commented Aug 28, 2023 at 4:26
-1

Here is a very simple Python Sublime Text build system that works when python scripts are invoked with py file_name.py.

Just create py.sublime-build by Tools > Build System > New Build System and add the contents below:

{
    "cmd": ["python3", "$file"]
}

You can select it in Sublime Text editor by going to Tools > Build System > py and building with Ctrl + b.

Note: If your filesystem doesn't python3 than you need to provide path/to/python3 and it should work.

1
  • Did you, by chance, actually try to use this build system? Clearly not, because it won't work. See the other, more helpful answers on this page.
    – MattDMo
    Commented Jul 19, 2020 at 0:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.