Sublime Forum

Does Python f-strings work?

#1

I saw someone in a youtube video use f-strings in sublime, but I can’t get it to work; it just shows up as a syntax error. Does anyone know why?

0 Likes

#2

which version of the python interpreter are you using to execute your python code?

0 Likes

#3
0 Likes

#4

Of course you can’t get it to work as the embedded python version is the the ancient sublime 3.3.6 and f-strings were introduced in 3.6.

I’ve said few times in some old-threads upgrading to a “proper” modern python version would be an extremely cool feature, it sucks to be stuck on 3.3.x ecosystem :frowning:

About the youtube video’s you’ve seen it was probably some build system or plugin spawning a process that used a modern python interpreter >=3.6, that’s pretty much…

So yeah SublimeHQ! I’m aware this feature request will be a hell of hard job but pleeeease, give us some modern python interpeter to play with hehe! :wink:

Ps. About f-strings, here’s PEP0498

0 Likes

#5

I’m using python 3.7.2, but I think someone said that all macs (which is what I’m using) come with python 2.7. I’m quite new to python, so I’m not sure how I would fix this. I do see that the syntax is highlighted though. If I do print(5 / 2), it also shows as 2, which is a python 2 thing

0 Likes

#6

I assume that you are using the Build System. You have to set it to Python 3 or it defaults to 2.7.]
i.e. “build with”. I also assume that your program is correct. Perhaps you could post it,

0 Likes

#7

I’m not sure how to set the build system to python 3; if I click the “build with” button in the tools menu it only shows Python and Python - Syntax Check. Sorry, what do you mean by program?

0 Likes

#8

Sublime ships with a build system that executes the command python in order to run a Python program, and that build also includes a variant that lets you syntax check your code (i.e. compile only).

Depending on your system system, python could execute Python 2 or it could execute Python 3. In the particular case of MacOS, as you mentioned Python 2 generally comes with the system, so if you want to use Python 3 you have to install it yourself.

Since Python 2 and Python 3 are not cross-compatible, if you have them both installed at the same time you generally end up in a situation where python executes Python 2 and you need to execute python3 in order to run Python 3.

I’ve never installed Python manually on my Mac before, but I have seen mentions that if you use homebrew, you end up in a situation where python2 and python3 are used to run those specific versions, while python may represent Python 3 directly (that is, it sets things up to assume Python 3 as the default).

If it doesn’t do that, then along with installing Python 3, you would also need to create a build system that knows to use python3 instead of python. To do that, use View Package File from the command palette to open Python/Python.sublime-build, then use Tools > Build System > New Build System... from the menu. Copy the contents of the file you opened and replace the full contents of the new build system with it, then change python to python3 and save the file as something like Python3.sublime-build.

Now when you use Build With... the menu will offer two additional choices, and you can pick the appropriate Python 3 build to run your program that way. On the other hand if python executes Python 3, the internal build system will work for you directly.

How you actually go about installing Python 3 on MacOS is something you’d have to Google.

1 Like

#9

Yayyyyyyyyy Thank you so much it works now :smile: I’m really not sure why installing python3 doesn’t change “python” from python2 to python3 :thinking:

0 Likes

#10

On Linux, python is usually Python 2 and if you install Python 3, it installs as python3 so that things can be distinct. I get mildly uneasy about how homebrew seems to want to make python be Python 3 (along with making a python2 and python3 so you can be explicit) because it seems like that is asking for trouble from any script that requires Python 2 and was just using python.

However, I’m by no means a Python guru, so I’m not entirely sure what’s going on there or why that’s considered a good idea. :smiley:

0 Likes

#11

I never use Homebrew on Mac to install Python. The official installer from Python installs it just fine.

1 Like

#12

Sorry, I mean your code. I come from an earlier time. I thought that you had Python 3 installed.
I m happy that you got it working.

0 Likes

#13

It’s fine :smile: Thanks for suggesting!

0 Likes