![](/img/trans.png)
[英]Why do I get 2 error messages when writing Python code using Visual Studio Code?
[英]Why do I get SyntaxError when running a Python 3 script on Visual Studio Code?
我正在完成Zed Shaw 的 Learn Python 3 the Hard Way的练习,但在练习 6 中,我遇到了一个语法错误,我无法弄清楚。 我尝试在 Google、StackOverflow(其他帖子)上进行搜索,但没有提到的解决方案对我有用。
引发此错误的代码片段是:
types_of_people = 10
x = f"There are {types_of_people} types of people."
print(x)
我在 macOS Mojave 10.14.6 上使用 Visual Studio Code 1.38.1,Python 3.7.4 64 位。
令人惊讶的是,我以三种不同的方式执行代码,其中两种方法显示相同的错误,但第三种方法实际上成功地执行了代码。 我试图了解为什么 VSCode 无法执行 python 脚本。 任何帮助将不胜感激。
使用标准方式在 VSCode 中执行 python 脚本:
这个方法给出了 SyntaxError。 错误 output 是:
[Running] python -u "/Users/e139177/Documents/Programming/Learn-Programming/tempCodeRunnerFile.py"
File "/Users/e139177/Documents/Programming/Learn-Programming/tempCodeRunnerFile.py", line 2
x = f"There are {types_of_people} types of people."
SyntaxError: invalid syntax
[Done] exited with code=1 in 0.035 seconds
屏幕截图 1 显示了 VS Code 中的错误。
在 VS Code 中使用了“在终端中运行 Python 文件”选项。
该方法执行脚本成功,生成的output为:
KENMACC02XG4AEJHD2:Learn-Programminge139177$/usr/local/bin/python3/Users/e139177/Documents/Programming/Learn-Programming/Exercise6.py
There are 10 types of people.
KENMACC02XG4AEJHD2:Learn-Programming e139177$
屏幕截图 2 显示了在 VS Code 终端中成功执行的脚本。
方法三
使用MacOS终端直接执行python脚本,不使用VSCode。
这个方法也给出了相同的 SyntaxError。 错误 output 是:
KENMACC02XG4AEJHD2:Learn-Programming e139177$ python Exercise6.py
File "Exercise6.py", line 2
x = f"There are {types_of_people} types of people."
^
SyntaxError: invalid syntax
KENMACC02XG4AEJHD2:Learn-Programming e139177$
屏幕截图 3 显示了在 VS Code 终端中成功执行的脚本。
我不确定为什么脚本在 VSCode 终端中运行时会成功执行,但是在使用 VSCode 的“运行”命令执行时,或者直接在 macOS 终端中执行时,它不会这样做。
In Method 1 you are using the Code Runner extension, not the Python extension and so it is simply using python
and not the Python interpreter you have selected for the Python extension. 方法 3 失败,因为python
传统上是 Python 2 ,除非您有激活的虚拟环境,并且 macOS 仅默认安装 Python 2 。
要解决方法 1,您必须适当地设置 Code Runner。 对于方法 3,您可以为 Python 3 使用虚拟环境,当它被激活时,您将在运行python
时得到您所期望的。
方法 1和方法 3的主要问题是您使用 Python 2.7 来运行您的应用程序,并且f 字符串仅从 Python 3.6 开始可用。
基本上,您需要确保您使用的是 Python 3.6+ 解释器。 Brett Cannon 的答案几乎总结了如何做到这一点,但这是该答案的扩展形式。
终端 output 的"[Running]"
部分表示您正在使用Code Runner (请注意扩展页面的屏幕截图也显示"[Running]"
)。 Code Runner 有一个Executor Map ,它设置了它如何运行不同语言的代码:
配置
确保在环境变量中设置了每种语言的执行程序 PATH。 您还可以在
code-runner.executorMap
中添加条目以设置执行程序PATH
。 例如,为 ruby、php 和 HTML 设置执行程序PATH
:{ "code-runner.executorMap": { "javascript": "node", "php": "C:\\php\\php.exe", "python": "python", "perl": "perl", "ruby": "C:\\Ruby23-x64\\bin\\ruby.exe", "go": "go run", "html": "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"", "java": "cd $dir && javac $fileName && java $fileNameWithoutExt", "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" } }
默认情况下,它只使用python
,根据您的系统,它可以是 Python 2 或其他一些不是您想要使用的正确版本的版本。
您需要在设置中更改它。
单击“在 settings.json 中编辑”,找到python
的条目:
"code-runner.executorMap": {
...
"python": "python -u",
...
并将其更改为正确的 Python 版本:
"code-runner.executorMap": {
...
"python": "/usr/local/opt/python@3.8/bin/python3 -u",
在上面的示例中,我将其设置为随 Homebrew 一起安装的 Python3.8:
$ brew info python@3.8
...
Python has been installed as
/usr/local/opt/python@3.8/bin/python3
然后检查 Code Runner 现在是否使用正确的路径:
[Running] /usr/local/opt/python@3.8/bin/python3 -u "/path/to/test.py"
There are 10 types of people.
使用标准方式在 VSCode 中执行 python 脚本
实际上,使用 Code Runner 并不是“标准方式”。 你实际上并不需要它。 Visual Studio Code 中 Python上的 VS Code 文档并没有告诉您安装它。 只要你select 正确的 Python 环境,你可以从底部的状态栏检查,你只需点击绿色的播放按钮或使用“在终端中运行 ZA7F5F35426B9274117FC9231B563”,它应该是正确的版本。
这就是方法2成功运行的原因。
我在 MacOS Mojave 10.14.6 上使用...与 Python 3.7.4 64 位。
Mac OS 内置了 Python 2.7,它是您使用python
时使用的一个:
Q$ python -V
Python 2.7.16
Q$ python test.py
File "test.py", line 2
x = f"There are {types_of_people} types of people."
^
SyntaxError: invalid syntax
您需要使用python3
或任何正确的解释器,具体取决于您如何安装 Python 3.x。 如果您是Homebrew 的 Python 3.x :
可执行文件的组织如下:
python3
指向 Homebrew 的 Python 3.x(如果已安装)pip3
指向 Homebrew 的 Python 3.x 的 pip(如果已安装)
如果您有多个版本,最好给它们起别名:
Q$ alias python3.7=/usr/local/opt/python@3.7/bin/python3
Q$ alias python3.8=/usr/local/opt/python@3.8/bin/python3
Q$ alias python3.9=/usr/local/opt/python@3.9/bin/python3
Q$ python3.7 -V
Python 3.7.9
Q$ python3.8 -V
Python 3.8.6
Q$ python3.9 -V
Python 3.9.1
Q$ python3.7 test.py
There are 10 types of people.
Q$ python3.8 test.py
There are 10 types of people.
Q$ python3.9 test.py
There are 10 types of people.
使用虚拟环境也是一种最佳实践方法:
Q$ python3.8 -m venv myvenv
Q$ source ./myvenv/bin/activate
(myvenv) Q$ python -V
Python 3.8.6
(myvenv) Q$ python test.py
There are 10 types of people.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.