bennimueller
bennimueller

Reputation: 31

f-Strings in Sublime Text

If I check the version of Python my Terminal says its 3.8.1. But if I try using these f strings in Sublime Text I always get a syntax error.

Can anyone tell me why?

So here is my version, related to my terminal

Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

This is the code:

greeting = 'Hello'
name = 'Hannah'

message = f'{greeting}, {name}. Welcome!'

print(message)

and this is the error:

File "/Users/bennimueller/Desktop/Intro.py", line 4
    message = f'{greeting}, {name}. Welcome!'
                                            ^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/bennimueller/Desktop/Intro.py"]
[dir: /Users/bennimueller/Desktop]
[path: /Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

Upvotes: 3

Views: 834

Answers (2)

rob
rob

Reputation: 11

For Python 3.9 its :

1. Go to Sublime Text to: Tools -> Build System -> New Build System and put the next lines:

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

Then save it with a meaningful name like: python3.sublime-build

2. Go to Tools -> Build system -> and check python3

Test it with:

import sys
print(sys.version)

Press: Ctrl + b

Upvotes: 1

MattDMo
MattDMo

Reputation: 102902

When you install Python from the python.org installer on macOS, the executable is actually called by running python3. Your build system is actually calling /usr/bin/python, the version of Python 2 that comes with macOS.

To get around this, you just create a custom build system. Go to ToolsBuild SystemNew Build System… (all the way at the bottom) and enter the contents below (delete all the placeholder template that comes up).

{
    "cmd": ["/Library/Frameworks/Python.framework/Versions/3.8/python3", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

When you go to save the file, it will automatically put you in the proper directory -- it's /Users/YourUserName/Library/Application Support/Sublime Text 3/Packages/User if you're interested. Name the file Python 3.sublime-build, and there will now be a ToolsBuild SystemPython 3 option.

Upvotes: 3

Related Questions