The Startup
Published in

The Startup

Setting Your Python Version in Sublime-Text

Ahh…”Just right!”

photo by Mercelo Gerpe at freeimages.com

Finding the right IDE is like stepping into a “The Three Bears” storybook. This one is too simple, this one is too complicated, this one is ‘Just Right’…except for the annoying fact that it’s using Python 2.7.

At least that was my issue with Sublime. I’ve tried other editors and they all work fine, but for some reason I like the look and feel of Sublime. I an attempt to rectify my issue, I of course turned to DuckDuckGo and found the internet was largely silent on an answer. After an hour of searching, messing with the program, and changing C++ build files, I finally hit those two keys “Ctrl+B” and my “F string literal” actually worked.

If you are in the same boat, let me save you some time and frustration.

First, if you are on a Linux machine or Mac, follow below. If you are running Windows, I’m sorry…

See which version of Python is running in Sublime-Text

Open Sublime, save a test file and run the following:

import sys
print(sys.version)

The output will be something like:

3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0]
[Finished in 0.0s]

If you are running the version you want, congrats! If not lets fix it.

Find your Python path

Open up a terminal and run:

which python
which python3

Your output will likely be /usr/bin/python2.7 or something similar. Now to find all the versions of Python on your system currently run:

ls path_to_python/python* | grep python*

In my case, this is:

ls /usr/bin/python* | grep python*

and I get this mess:

/usr/bin/python
/usr/bin/python2
/usr/bin/python2.7
/usr/bin/python3
/usr/bin/python3.6
/usr/bin/python3.6-config
/usr/bin/python3.6m
/usr/bin/python3.6m-config
/usr/bin/python3.7
/usr/bin/python3.7m
/usr/bin/python3.8
/usr/bin/python3-config
/usr/bin/python3m
/usr/bin/python3m-config

Now if you are in the same boat, its probably time to learn about virtualenv. In my case, I’ve been lazy with this Ubuntu box and now I’m in this situation.

I want to run Python3.6 in my Sublime-Text application. To do this I need to change my python.sublime build file:

Open Sublime-Text and then select:

Open Preferences > Package Control > Install Package

Now select PackageResourceViewer and this will install it.

Now we need to update the build file. Select the following:

Tools > Command Pallete > type 'prv' > PackageResourceViewer: Open Resource > python > python.sublime build

Change the “shell_cmd”: “pythonX.X” to your desired python version. Be sure to change the “variants shell_cmd” to the same.

Save the file, re-run your test file and you should now see the correct version being run.

import sysprint(sys.version)x = 'Now'
y = 'just right'
print(f'{x} that is {y}!')3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0]
Now that is just right!
[Finished in 0.0s]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
MB

MB

Husband, Father, Pediatrician & Informaticist writing about whatever is on my mind for today.