31

I followed these steps to run Python 3 on Sublime Text 3.

Select the menu Tools > Build > New Build System and I entered the following:

{
"cmd": ["python3", "$file"]
, "selector": "source.python"
, "file_regex": "file \"(...*?)\", line ([0-9]+)"
}

After that, I saved it to the following (Mac-specific) directory: ~/Library/Application Support/Sublime Text 3/Packages/User

but I'm getting this error when I'm trying to run my code on Python 3 in Sublime:

[Errno 2] No such file or directory: 'python3'
baduker
  • 19,152
  • 9
  • 33
  • 56
user3555502
  • 339
  • 1
  • 4
  • 7
  • `python3` should be in system path. – Kenly Jan 31 '16 at 20:16
  • See my answer already posted [here](https://stackoverflow.com/questions/23730866/set-up-python-3-build-system-with-sublime-text-3/46132747#46132747). – Daniel Sep 09 '17 at 16:57

3 Answers3

40

You need to provide the full path to python3, since Sublime Text does not read your ~/.bash_profile file. Open up Terminal, type which python3, and use that full path:

{
  "cmd": ["path/to/python3", "$file"], 
  "selector": "source.python", 
  "file_regex": "file \"(...*?)\", line ([0-9]+)"
}
duan
  • 8,515
  • 3
  • 48
  • 70
Andrew
  • 36,541
  • 13
  • 67
  • 93
  • 2
    This `Sublime Text does not read your ~/.bash_profile file` explains a heap of things I have been struggling with. Should have realised it but I didn't. Awesome clarification. Thank you! – jwpfox Apr 25 '14 at 00:01
  • When you paste the path_to_python in the above code and save it as a Build System File. 1.) Don't forget to give it an easy to remember name (i.e Python3) 2.) Go back to Build System and switch the Build to the one you just created. Everything should work correctly. – Trapp Sep 05 '17 at 00:25
25

This is the snippet that I've been using. It's a slight variation to Andrew's solution, such that python3 is dynamically located by consulting the UNIX environment's PATH setting (not unlike how you would do the same inside a Python shell script; e.g.: '#! /usr/bin/env python3').

This snippet also uses "shell_cmd" instead of "cmd", which sublime-text-3 has seemingly switched to.

{
    "shell_cmd": "/usr/bin/env python3 ${file}",
    "selector": "source.python",
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "working_dir": "${file_path}",
}

I saved mine in ".../Packages/User/Python3.sublime-build". I hope this helps you. =:)

NYCeyes
  • 5,215
  • 6
  • 57
  • 64
  • 1
    I found this really useful. – james-see Aug 17 '15 at 13:49
  • Need an enclosing `''` i.e. `'${file}'` in case the file path contains whitespace. Might need to do the same for "${file_path}"? And also if python3's location contains whitespace, maybe there is a problem. Goddamn whitespace-in-filepaths. – P i Oct 27 '15 at 12:38
  • 1
    I tried `which python3` and it shows `/usr/bin/python3` is where my python3 is. The rest is working. – Henry J Jan 15 '17 at 23:11
  • why does output: /usr/local/opt/python/bin/python3.7 instead of my selected: /usr/local/bin/python3.7 – Galapagos Oct 27 '19 at 07:35
-1

Thanks for your question. I began to learn python a couple days ago, and I am stuck with the same problem that you have met.Just as Andrew said above,it is “path problem”. I would like to share the code that I used to get python3 on sublime3. For MacOS user:

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

and save the file as Python3.sublime-build. I deeply recommended the book "A byte of python " for the new beginner of python.This book contributes a lot to my answer for this question.

David_L
  • 1
  • 1