如果我检查 Python 的版本,我的终端显示它是 3.8.1。但是,如果我尝试在 Sublime Text 中使用这些 f 字符串,我总是会遇到语法错误。
谁能告诉我为什么?
这是我的版本,与我的终端相关
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.
这是代码:
greeting = 'Hello'
name = 'Hannah'
message = f'{greeting}, {name}. Welcome!'
print(message)
这是错误:
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]
最佳答案
当您在 macOS 上从 python.org 安装程序安装 Python 时,可执行文件实际上是通过运行 python3
调用的.你的build system实际上是在调用 /usr/bin/python
,macOS 附带的 Python 2 版本。
要解决这个问题,您只需创建一个自定义构建系统。转到 Tools
→ Build System
→ New Build System…
(一直在底部)并输入下面的内容(删除所有出现的占位符模板)。
{
"cmd": ["/Library/Frameworks/Python.framework/Versions/3.8/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
当你去保存文件时,它会自动把你放在正确的目录 -- 它是 /Users/<b><i>YourUserName</i></b>/Library/Application Support/Sublime Text 3/Packages/User
如果你有兴趣。将文件命名为 Python 3.sublime-build
, 现在会有一个 Tools
→ Build System
→ Python 3
选项。
关于python - Sublime Text 中的 f-String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60383223/