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.

3 Likes

#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

#14

Hi there!

I have the same issue, where can i find the command palette?

using macOS, sublime text and python 3.8 (which i downloaded from python.org)

0 Likes

#15

The command palette is available from the main menu under Tools > Command Palette (the menu will show you what key sequence opens this up for you as well). The list of commands is quite large, so you can enter some text to filter the results to make it easier to find the file you want.

1 Like

#16

thanks, and when you say replace python with python3 should i replace all the python's?
copied code:
{
“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}\"",
	}
]

}

0 Likes

#17

Yeah realistically the only parts that you need to change are to modify python to be python3 (or whatever it is you need to run the correct version of Python); everything else is what tells Sublime what the environment to run the program is, what the error looks like and how to select the build system automatically, etc; so that should stay as is.

0 Likes

#18

i see, yet it doesn’t seem to work…

what got me on this thread was this error:

I think it has to do with the version of python yet i followed your step by step explanation and still get the same syntaxerror: invalid syntax

would be great if someone knows what is wrong here.

1 Like

#19

You can verify if the correct version of Python is being run by executing a script like:

import sys
print(sys.version)

Note however that after adding a new build system you need to make sure that it’s selected. The easiest way to do that here is to make sure that under Tools > Build System that Automatic is checked to make Sublime pick the build system. Once you do that, choose Tools > Build With... to force Sublime to ask you to pick the build system, and make sure that you pick the one that you created from the list (it will show up based on the name you saved your new sublime-build file as).

1 Like

#20

Hi there guys, I recently found this thread while trying to fix annoying f-string invalid syntax issue. Any of proposed here Build System hacks didn’t helped. Aparently Sublime Text is using built in Python 3.3.6 for python syntax validation and this is freaking me out little bit, since many project is already using new syntax.

Any ideas how this can be fixed? Sublime Text was (and still is) my editor of a choice for years, and I would love to figure out this issue.

P.s. I’m using macOS, this may be part of the problem.

demo

0 Likes