1

我正在完成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。错误输出为:

[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 中的错误。

VSCode 标准运行命令

方法二

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

VSCode 终端输出

方法三

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

MacOS 终端输出

我不确定为什么脚本在 VSCode 终端中运行时会成功执行,但是在使用 VSCode 的“运行”命令执行时,或者直接在 macOS 终端中执行时,它不会这样做。

4

2 回答 2

1

The main problem with Method 1 and Method 3 is that you are using Python 2.7 to run your app, and f-strings are only available starting on Python 3.6.

Basically, you need to make sure you are using a Python 3.6+ interpreter. The answer from Brett Cannon pretty much summarizes how to do that, but here is the expanded form of that answer.


Method 1: Code Runner

The "[Running]" part of the terminal output indicates you are using Code Runner (notice the screenshots of the extension page also show "[Running]"). Code Runner has an Executor Map, which sets how it runs the code for different languages:

Configuration

Make sure the executor PATH of each language is set in the environment variable. You could also add entry into code-runner.executorMap to set the executor PATH. e.g. To set the executor PATH for ruby, php and 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"
    } 
}

By default, it just uses python, which, depending on your system, can be Python 2 or some other version that is not the correct version you want to use.

You'll need to change it in the settings.

settings code runner

On clicking "Edit in settings.json", find the entry for python:

    "code-runner.executorMap": {
        ...
        "python": "python -u",
        ...

And change it to the correct Python version:

    "code-runner.executorMap": {
        ...
        "python": "/usr/local/opt/python@3.8/bin/python3 -u",

In the example above, I set it to the Python3.8 installed with Homebrew:

$ brew info python@3.8
...
Python has been installed as
  /usr/local/opt/python@3.8/bin/python3

Then check that Code Runner now uses the correct path:

[Running] /usr/local/opt/python@3.8/bin/python3 -u "/path/to/test.py"
There are 10 types of people.

Used the standard way to execute python script in VSCode

Actually, using Code Runner isn't the "standard way". You don't actually need it. The VS Code docs on Python in Visual Studio Code does not tell you to install it. As long as you select the correct Python environment, which you can check from the status bar at the bottom, you can just click on the green play button or use "Run Python File in Terminal", and it should use the correct version.

vs code

It's the reason why Method 2 ran successfully.

Method 3: From Terminal

I am using ... with Python 3.7.4 64-bit on MacOS Mojave 10.14.6.

Mac OS comes with Python 2.7 as a built-in, and it is the one used when you use 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

You need to use python3 or whichever is the correct interpreter depending on you how installed Python 3.x. If you are Homebrew's Python 3.x:

The executables are organised as follows:

  • python3 points to Homebrew’s Python 3.x (if installed)
  • pip3 points to Homebrew’s Python 3.x’s pip (if installed)

If you have multiple versions, it's a good idea to alias them:

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.

Using virtual environments is also a best practice approach:

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.
于 2020-12-13T07:18:14.217 回答
1

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

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

于 2019-09-30T23:37:12.210 回答