Using Multiple Versions of Python with Sublime Text 3

Programming

 

Note: This post is strictly about setting up Sublime Text 3 to build and run Python scripts with multiple versions of Python installed. IDE’s such as PyCharm are outside the scope of this topic.

One of the most irritating things about coding Python or other interpreted languages in a text editor is the lack of ability to build or run the program. Programming in a text editor like Notepad or similar requires the use of multiple applications to create and test your code. For instance, you need to have your editor open to write your code and a console of your choice to run and test the code. This can be a bit of a pain to a programmer’s workflow, constantly switching back and forth and entering commands to see a result.

This problem is further compounded when working on a Windows system with multiple versions of Python, like Python 2.7 and 3.5. In this situation where there isn’t a Python PATH variable set, you have to use the Py Launcher to run scripts:

# py -v script.py where -v denotes Python version number ie -2, -2.7, -3, -3.5
> py -3 script.py
# -v can be omitted when using "shebangs", which is explained later.

Sublime Text Support

For users of Sublime Text, though, this is a non-issue. ST comes with a set of pre-made extensions that allow a programmer to quickly build many different types of languages. The results, errors, and build time will be displayed in the built in build console.

kfy9enu

Cuda-C++ is not included by default, but installed with the CUDA plugin

For a user with a PATH variable set for Python, everything should be good to go!

But, for those like me  with multiple installations and no PATH, we have some work to do. We’ll look at how to set up Sublime to use the Py Launcher. First, we need to access a build file. One can either create a new build system, or modify a default built-in build file.

Create a New Build System

This is useful for if you want to have separate builds for different Python versions or for builds that accomplish different tasks. For those who want to go that route, I’ve placed the base Python build code below (as of Build 3114). Make sure you save your build as BuildName.sublime-build and it is placed in your AppData\Roaming\Sublime Text 3\Packages directory.

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

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

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

Accessing a Default Build System

The other way is to edit the original Python build file, which is what I’ve done and what I’ll show you. To do this, there are a few requirements. As Sublime’s build files are stored in .sublime-package archives, we’ll need a plugin that can open these (or an archive manager such as 7-Zip). First, you need to have Package Control installed to easily add and maintain any plugins Sublime Text. Once you have done that, ctrl+shift+P to bring up the Command Palette or go to Tools > Command Palette. Type install and select Package Control: Install Package. From there, search for PackageResourceViewer (PRV) and press enter to install the plugin.

After you have PRV installed open up the Command Palette again, type prv then select PackageResourceViewer: Open Resource. Search for Python and then build. Select Python.sublime-build to open the file. If you went the way of using an archive manager like 7-Zip, the build files can be found by opening the Python.sublime-package in C:\Program Files\Sublime Text 3\Packages.

Editing the Build File

Once you have the build file open (regardless of method used above), there is only one word that needs to be changed. In the first line change python to py , press save and you’re done! Ignore the "variants" section, as it’s unnecessary to change. For a while I had “variants” changed and not the overall “shell_cmd” and it would not let me build GUI applications.

Running your script from Sublime Text

Now you can ctrl+B to your heart’s content and watch builds fly by without ever leaving the app. It should be noted that Sublime Text’s build console has one minor/major limitation: It does not accept user input. So if you have input() or raw_input() anywhere in your script, the build system will fail. You will have to go back to the command line and use the py script.py command for this to work.*

How does py launcher and Sublime Text know what version of Python to use? The magic is in the shebang line. At the top of each file that can be executed independently, use the line #! pythonX where the X is the version of Python you wish to use. Based on this shebang value, the py launcher will determine the correct version of Python to use. Otherwise, it will pick it’s own version to use, for me that’s 2.7. If you still need to run scripts from a command line, you can simply use py script.py when using the shebang method.

To confirm this is indeed working, from a command line run the following script with and without shebang lines using the py command:

#! pythonX
import sys
print(sys.version)

If you have any questions, concerns, or suggestions regarding the topic at hand feel free to leave a comment down below! For those looking for even greater Python support in Sublime Text 3, please check out this post by Real Python on Setting Up Sublime Text 3 for Full Stack Python Development.

* It is possible for you to use the Python Interactive Shell with support for user input in Sublime Text 3 through the use of SublimeREPL, but that will be for a future post.

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s