【问题标题】:Why do I get SyntaxError when running a Python 3 script on Visual Studio Code?为什么在 Visual Studio Code 上运行 Python 3 脚本时会出现 SyntaxError?
【发布时间】:2019-09-28 21:57:32
【问题描述】:

我正在完成 Zed Shaw 的 Learn Python 3 the Hard Way 的练习,但在练习 6 中,我遇到了一个语法错误,我无法弄清楚。我尝试在 Google、StackOverflow(其他帖子)上进行搜索,但提到的解决方案都不适合我。

抛出此错误的代码的 sn-p 是:

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。错误输出为:

[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 文件”选项。

该方法成功执行脚本,生成的输出为:

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 终端中成功执行的脚本。

方法3

使用MacOS终端直接执行python脚本,不使用VSCode。

这个方法也给出了相同的 SyntaxError。错误输出为:

 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 终端中执行时,它不会这样做。

【问题讨论】:

  • 你确定你的“运行”按钮连接的python版本是3.7吗? f-strings 仅在 3.6 中引入
  • 让-弗朗索瓦·法布尔您好。是的,我确定连接的版本是 3.7.4 64 位。原因是我在 VSCode 中选择了 Python Interpreter 作为 Python 3.7.4 64 位,您还可以在我附在屏幕左下角的第一个屏幕截图中看到 VSCode 中活动的版本。
  • 运行import sys; print(sys.version),你可能会发现它实际上不是3.7.4。
  • 你是对的 user2357112。非常感谢你做的这些。在这种情况下,我如何确保它是 pythong 3.7.4?这是我得到的结果: >>> import sys; print(sys.version) 2.7.10 (default, Feb 22 2019, 21:55:15) [GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] >>>

标签: python visual-studio-code syntax-error python-3.7


【解决方案1】:

在方法 1 中,您使用的是 Code Runner 扩展,而不是 Python 扩展,因此它只是使用 python 而不是您为 Python 扩展选择的 Python 解释器。方法 3 失败,因为 python 传统上是 Python 2,除非您有激活的虚拟环境并且 macOS 仅默认安装 Python 2。

要解决方法 1,您必须适当地设置 Code Runner。对于方法 3,您可以使用 Python 3 的虚拟环境,当它被激活时,您将在运行 python 时获得您所期望的结果。

【讨论】:

  • 谢谢布雷特。我知道这是一个迟到的回应,但想感谢您的投入。我还没有机会查看您的回复,所以会这样做并更新。
【解决方案2】:

方法 1方法 3 的主要问题是您使用 Python 2.7 来运行您的应用程序,而 f-strings 是仅从 Python 3.6 开始可用。

基本上,您需要确保您使用的是 Python 3.6+ 解释器。 answer from Brett Cannon 几乎总结了如何做到这一点,但这是该答案的扩展形式。


方法一:代码运行器

终端输出的"[Running]" 部分表示您正在使用Code Runner(注意扩展页面的屏幕截图也显示"[Running]")。 Code Runner 有一个Executor Map,它设置了它如何运行不同语言的代码:

配置

确保在环境中设置了每种语言的执行器PATH 多变的。您还可以在code-runner.executorMap 中添加条目以设置 执行者PATH。例如为 ruby​​、php 和 HTML:

{
    "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 并不是“标准方式”。你实际上并不需要它。 Python in Visual Studio Code 上的 VS Code 文档并没有告诉您安装它。只要你select the correct Python environment,你可以在底部的状态栏中查看,你只需点击绿色的播放按钮或使用“在终端中运行Python文件”,它应该使用正确的版本。

这就是方法2运行成功的原因。

方法3:从终端

我在 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's 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.

使用virtual environments 也是一种最佳实践方法:

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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多