Sublime Forum

Beginner Question about running Python 3 within Sublime Text

#1

Hi everyone,

Just starting to learn python and have been using Corey Schafers youtube videos to enhance my skills. For some reason, even though I’ve downloaded and installed Python 3.7.3 on my Macbook, sublime runs what seems to be an older version of python (2.7 as that what was on my macbook natively).

I know this because when I type in print(6/5) and run my script, it outputs 1, which is what Python did during its 2.x versions.

Does anyone have an idea why it is doing this? In my terminal, when I input python --version it outputs Python 3.7.3, so I’m not sure why sublime would not be running the latest python I’ve installed.

I’m completely new to this, so please excuse me if this is a silly question.

Thanks!

0 Likes

#2

At a guess I would say that it’s a PATH issue of some sort.

The build system that ships with Sublime by default just tries to execute python (specifically python -u) in order to execute a Python script. If that’s finding a python that’s a 2.x version while in the terminal python executes a 3.x version, then it seems like the terminal is using a different PATH than Sublime, one in which version 2 is found before version 3.

Paths on MacOS are a bit of a bummer because GUI applications get their PATH set differently than programs executed in the terminal do. Sublime mitigates that by attempting at startup to fetch the path from your shell, but that can go wrong if doing so takes too long. You can verify that one way or the other by opening the Sublime console with View > Show Console and scrolling up; near the top there is a line that either says that it got info from your shell or that there was no output because it took too long.

If you installed Python 3 while you had Sublime running, then the path might have changed out from under Sublime, in which case quitting and restarting Sublime would make it pick up the new path. Similarly a reboot might also clear the problem up as well (or at least it’s a fairly quick and painless troubleshooting step :smile:).

When I install Python 3 on my Mac I tend to do it from the downloads available from the Python website, which gives me a python3 interpreter instead of a python interpreter, so that the two versions are distinct. It doesn’t sound like that’s what happened when you did it, though.

You might try executing python3 from the terminal and see if that also works and gives you a Python 3 interpreter. If it does, then it’s pretty easy to add a new build system that will use that instead of the other one, which we can help you with.

0 Likes