Sublime Forum

Having trouble getting Sublime Text configured to use Python3

#1

I’m trying to configure Sublime so that Python3 is the default setting but I can’t find it in Tools. Am I not looking in the right place? Is there a different version of Sublime that I should be using?

The goal is to have terminal output the following:

Hello Python World!
[Finished in 0.1s]

Any thoughts? Thanks for your time!

0 Likes

#2

I fear that you will have to install the Python 3 package, but perhaps someone else will have a better idea.

0 Likes

#3

The default Python package that ships with Sublime tries to run an executable called python in order to execute code. If running python from the console runs python 3, you should be good to go out of the box regardless of how the Python build system is the one selected.

That said, MacOS ships with python version 2 installed in /usr/bin/python, so if you’ve taken no further steps the default support for Python will run python 2 instead, which isn’t what you want. In order to get Python 3 support, you need to install python 3 as a first step (it is a common misunderstanding that since Sublime has an embedded version of Python 3 that you can use that to run your code, but this is not the case).

The installer for Python 3 for MacOS installs a binary named python3 so that it doesn’t conflict with the system default python, since the two versions are not compatible with each other. As a result of that, the default build system for Python will use python version 2 instead.

One way to fix this would be to use PackageResourceViewer to edit Python\Python.sublime-build so that it runs python3 instead of python and everything will work as you want it to. This video for my OverrideAudit package demonstrates doing exactly this (on Windows but the concept remains the same) at around the 7 minute mark if you’re unfamiliar with this process.

Another option would be to create a file named e.g. Python3.sublime-build in your User package (use Sublime Text > Preferences > Browse Packages... to find it) and fill it will the following text:

{
	"shell_cmd": "python3 -u \"$file\"",
	"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
	"selector": "source.python",

	"env": {"PYTHONIOENCODING": "utf-8"},

	"variants":
	[
		{
			"name": "Syntax Check",
			"shell_cmd": "python3 -m py_compile \"${file}\"",
		}
	]
}

With this file in place you will have new build option named Python3 (or whatever you name your sublime-build file) that runs python3 instead. This is the default python build file with the modifications mentioned above.

Note that in either case, Sublime needs to be able to execute python3 in order to execute the build. You may need to take extra steps to include the install location of python3 in your system path for this to work. On my machine it’s installed in /Library/Frameworks/Python.framework/Versions/3.6/bin/python3, but it’s also in my system path and it does not appear that I did anything special to make that happen.

In a pinch you can modify the build to use this path explicitly instead of just python3 to ensure that it gets found.

2 Likes

#4

dude, make sure you’ve saved the file with an end of “.sublime-build”
ex “Python3.sublime-build”
that works.

0 Likes