从 Visual Studio Code 内部运行 python 时出现无效语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51540391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:52:30  来源:igfitidea点击:

Invalid Syntax error when running python from inside Visual Studio Code

pythonpython-3.xvisual-studio-codesyntax-error

提问by Andree Wille

i have a python file with the following content saved on my machine:

我有一个 python 文件,我的机器上保存了以下内容:

types_of_people = 10
x = f"There are {types_of_people} types of people"

binary = "binary"
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}."

print(x)
print(y)

print(f"i said: {x}")
print(f"I also said: '{y}'")

hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"

print(joke_evaluation.format(hilarious))
w = "This is the left side of ..."
e = "a string with a right side."

print(w + e)

When i open this file with Python 3.7 from within Visual Studio Code i get the following error:

当我从 Visual Studio Code 中使用 Python 3.7 打开此文件时,出现以下错误:

/usr/local/opt/python/bin/python3.7 /Users/andree/Desktop/test.py
  File "<stdin>", line 1
    /usr/local/opt/python/bin/python3.7 /Users/andree/Desktop/test.py
    ^
SyntaxError: invalid syntax

In the following screenshot you can see the command i use to run the file and also which python extension i use.

在下面的屏幕截图中,您可以看到我用来运行文件的命令以及我使用的 python 扩展名。

run python file from within Visual Studio Code

从 Visual Studio Code 中运行 python 文件

But running the file from within my terminal with python3 test.pyworks just fine.

但是从我的终端中运行文件python3 test.py效果很好。

Does anyone know what the problem is when running it from within VS Code?

有谁知道从 VS Code 中运行它时的问题是什么?

采纳答案by Andree Wille

Looks like this is a bug in VS Code.

看起来这是 VS Code 中的一个错误。

When i create a new file, assign python language to it and then save it then it works when i run the python file from within the editor.

当我创建一个新文件时,为它分配 python 语言,然后保存它,然后当我从编辑器中运行 python 文件时它就可以工作了。

But when i create a new file, assign python langauge but dont save it, execute "Run Selection/Line in Python Terminal" afterwards save it and then run "Run Python file in Terminal" it doen't work. So this seems to be an VS Code related issue.

但是当我创建一个新文件时,分配python语言但不保存它,然后执行“在Python终端中运行选择/行”保存它然后运行“在终端中运行Python文件”它不起作用。所以这似乎是一个与 VS Code 相关的问题。

回答by Pavel

Think this is a bug of VS Code.

认为这是 VS Code 的一个错误。

When you use "run selection/line in python terminal" command, VS Code starts python interpreter and doesn`t quit it after completion.

当您使用“在 python 终端中运行选择/行”命令时,VS Code 会启动 python 解释器并且在完成后不会退出。

You should use exit()command in python interpreter window to end python session.

您应该exit()在 python 解释器窗口中使用命令来结束 python 会话。

After that "run python file in terminal" will work fine.

之后“在终端中运行 python 文件”将正常工作。

回答by amesj7595

The problem for me was that I accidentally used Shift+ Returnwhich executed the python program, when in fact I meant to hit CTRL+ Returnto move to the next line without touching the mouse.

对我来说,问题是我不小心使用了Shift+Return它执行了 python 程序,而实际上我打算点击CTRL+Return移动到下一行而不触摸鼠标。

Using exit()command in the console worked.

exit()在控制台中使用命令有效。

回答by SamOverflow

Disable terminal.integrated.inheritEnv in settings. This was suggested by VSCode for me and it worked.

在设置中禁用 terminal.integrated.inheritEnv。这是 VSCode 为我建议的,并且有效。

回答by yash

It's a probable bug in VS code. I don't know why there hasn't been a patch for this. After typing exit()in the terminal, the rerun should work fine. You could also try Ctrl+F5to run in a debug mode.

这是 VS 代码中的一个可能的错误。我不知道为什么没有针对此的补丁。在exit()终端中输入后,重新运行应该可以正常工作。您也可以尝试Ctrl+F5在调试模式下运行。

回答by user10209131

I found a fix for this, install "pylint". I had a pop-up message in Visual Studio that asked me to download this extension. I did and after that I was able to run my code!

我找到了解决方法,安装“pylint”。我在 Visual Studio 中有一条弹出消息,要求我下载此扩展程序。我做到了,之后我就可以运行我的代码了!

回答by TechBrad

I experienced this issue when attempting to change my default terminal settings. I continually ran into a situation where the "Run Python File in Terminal"command would result in syntax errors while the "Run Selection/Line in Python Terminal" command would error but still display the results. Irritating to say the least.

我在尝试更改默认终端设置时遇到了这个问题。我不断遇到“在终端中运行 Python 文件”命令会导致语法错误而“在 Python 终端中运行选择/行”命令会出错但仍会显示结果的情况。至少可以说令人恼火。

Here's the settings I utilized to resolve the syntax errors issue.

这是我用来解决语法错误问题的设置。

Note: Enabling Pylint did not resolve my issue, in fact it continued to pop-up even after selecting to enable it. These specific user/workspace/folder settings resolved that issue for me too.

注意:启用 Pylint 并没有解决我的问题,事实上,即使选择启用它,它仍然继续弹出。这些特定的用户/工作区/文件夹设置也为我解决了这个问题。

Note: Since the terminal defaults to Powershell, you have to type Python to enter manual commands directly into the python terminal and exit() to close it to allow the python file to run properly again.

注意:由于终端默认为Powershell,因此您必须键入Python 直接在python 终端中输入手动命令并exit() 将其关闭,以便python 文件再次正常运行。

USER SETTINGS

用户设置

{
"terminal.integrated.shell.windows": "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
}

WORKSPACE SETTINGS

工作区设置

"settings": {
    "terminal.integrated.shell.windows": "C:\Python3.7.2\python.exe",
}

FOLDER SETTINGS

文件夹设置

"python.linting.pylintEnabled": true,
"python.pythonPath": "C:\Python3.7.2\python.exe",

回答by killerG

I got the same issue, but the code ran for me when I ran it using 'Start without debugging'. This can also be done with the shortcut CTRL+ F5.

我遇到了同样的问题,但是当我使用“不调试启动”运行它时,代码为我运行。这也可以通过快捷键CTRL+来完成F5