1

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?

  • 1
    `"cmd": ["python3", "-u", "$file"]` doesn't ensure you're running Python 3.7. Try running a program that's just `import sys; print(sys.version)` and see what it prints. – user2357112 Aug 09 '20 at 01:49
  • Also, I can see some output in there that says you've probably got both Python 3.8 and 3.7 installed (on top of whatever default Python you're likely running instead). – user2357112 Aug 09 '20 at 01:52
  • @user2357112supportsMonica Thanks for the quick response! Sublime Text is mysteriously running in python2.7.16. However, reinputting ` "cmd": ["python3", "-u", "$file"] ` did not change it to python3. Do you have any idea how I can make the switch? Thanks! – David Jiang Aug 09 '20 at 01:56
  • For clarity, all Sublime does is ask an external thing (in this case Python) to run a file, so your problem isn't that "f-strings don't work in Sublime", it's that the wrong version of Python is being used. The diagnostic shows you what Sublime tried to run, which is not the build system you think it is. You probably want to select `Python3` from `Tools > Build System` to make sure that Sublime uses it. – OdatNurd Aug 09 '20 at 02:21
  • @OdatNurd I don't have a python3 option from the Build System options; I only have python. I ran the "cmd" program above to create a build system for python3, but there isn't an option for it after I ran the program. At this point, I think I'm going to reinstall sublime text and see what happens. – David Jiang Aug 09 '20 at 02:33
  • Reinstalling won't help; removing Sublime leaves your configuration intact. If you don't see `Python3` build in the menu, you didn't save `Python3.sublime-build` in the correct place or not with that name. The items in the menu mimic the names of the build files. – OdatNurd Aug 09 '20 at 03:36
  • Thank god, it finally worked! I can now sleep in peace. Thank you for all of your help! – David Jiang Aug 09 '20 at 04:15

3 Answers3

1

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

Akshat
  • 123
  • 10
  • Thanks for the quick response. However, since my book was able to use f-strings without any addons, it is some sort of problem on my end. I am hoping there is a solution without involving any extra text editors or add-ons. I will look into python Repl, though. Thank you for your input! – David Jiang Aug 09 '20 at 02:01
  • If you are using the inbuilt python in Sublime Text, it won't work as the inbuilt one does not work with inputs and F Strings. – Akshat Aug 09 '20 at 02:13
  • I think it has to do with what version of python I'm running. It turns out I am actually running python2.7.16 when I thought I was running python3.7, which is very strange. Also, are you sure that inbuilt python in Sublime Text won't work with F Strings? My book says it does, so I'm not really sure which is which anymore. – David Jiang Aug 09 '20 at 02:20
  • It will work if you are using the newest version of Python. Have a look at this article- https://forum.sublimetext.com/t/does-python-f-strings-work/43530 – Akshat Aug 09 '20 at 09:09
1

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 Packages\User directory)

Use ctrl+shift+b and select python3.

Next time ctrl+b will automatically use python3

0

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.

SPC0DE
  • 1