TransWikia.com

f-strings works on python IDLE but not on Sublime Text

Stack Overflow Asked by David Jiang on January 22, 2021

I have just started reading the 2nd edition of Python Crash Course, and just in case someone has the book, I am on the subsection "Using Variables in Strings" in chapter 2.

The problem is, when I enter

first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)

on Sublime Text, it is supposed to output

ada lovelace

but instead, I receive an error reading

  File "/Users/davidjiang/Code/Python/python_work/full_name.py", line 3
    full_name = f"{first_name} {last_name}"
                                          ^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/davidjiang/Code/Python/python_work/full_name.py"]
[dir: /Users/davidjiang/Code/Python/python_work]
[path: /Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

I have already checked that Sublime Text is running in python3.7 by inserting

{ 
    "cmd": ["python3", "-u", "$file"], 
}

into the build system Python3.sublime-build. My terminal also runs in python3.

I also made sure that my code didn’t have any errors by finding and using this link explaining how to insert values into strings.
Additionally, the book I am reading comes with a collection of online resources complementing the course, including the code written at the beginning. When I put it through python IDLE, the code worked out fine.

Can someone please explain why the code doesn’t work on Sublime Text? Thanks in advance!

Edit: Apparently, my Sublime Text is running python2 instead of python3. As it seems that inputting the above code won’t work, does anyone have any advice on how to change Sublime to python3?

3 Answers

In you error output, it's clearly written

[shell_cmd: python -u "/Users/davidjiang/Code/Python/python_work/full_name.py"]

The key thing here is that the shell command is python not python3. So you aren't running the proper build system.

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

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "python3 -m py_compile "${file}"",
        }
    ]
}

Save this as python3.sublime-build (in the PackagesUser directory)

Use ctrl+shift+b and select python3.

Next time ctrl+b will automatically use python3

Answered by Nawfal Ahmed on January 22, 2021

There's a way to download python 3 to sublime text. First you want to go to the build system option then scroll all the way down to "New Build System". From there it will open up a new tab and will display this: { "shell_cmd": "make" }. Delete that and replace it with:

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

then save, naming it whatever you'd like (make sure it saves as a .sublime-build file). Now you should have it in your build systems. If it is not there after you save you may have to restart the program.

Answered by SPC0DE on January 22, 2021

Using python Repl should work as this can run F strings and inputs. Make sure you are using this as an add on otherwise this will not work. Using this should help- https://packagecontrol.io/packages/SublimeREPL

Answered by Akshat on January 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP