简体   繁体   English

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

[英]Invalid Syntax error when running python from inside Visual Studio Code

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 扩展名。

从 Visual Studio Code 中运行 python 文件

But running the file from within my terminal with python3 test.py works just fine.但是在我的终端中使用python3 test.py运行该文件效果很好。

Does anyone know what the problem is when running it from within VS Code?有谁知道从 VS Code 中运行它时的问题是什么?

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.您应该在 python 解释器窗口中使用exit()命令来结束 python 会话。

After that "run python file in terminal" will work fine.之后“在终端中运行 python 文件”将正常工作。

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.但是当我创建一个新文件时,分配python语言但不保存它,然后执行“在Python终端中运行选择/行”保存它然后运行“在终端中运行Python文件”它不起作用。 So this seems to be an VS Code related issue.所以这似乎是一个与 VS Code 相关的问题。

The problem for me was that I accidentally used Shift + Return which executed the python program, when in fact I meant to hit CTRL + Return to move to the next line without touching the mouse.我的问题是我不小心使用了执行 python 程序的Shift + Return ,而实际上我打算按CTRL + Return以在不触摸鼠标的情况下移动到下一行。

Using exit() command in the console worked.在控制台中使用exit()命令有效。

It's a probable bug in VS code.这是 VS 代码中的一个可能的错误。 I don't know why there hasn't been a patch for this.我不知道为什么没有针对此的补丁。 After typing exit() in the terminal, the rerun should work fine.在终端中输入exit()后,重新运行应该可以正常工作。 You could also try Ctrl+F5 to run in a debug mode.您也可以尝试Ctrl+F5在调试模式下运行。

Disable terminal.integrated.inheritEnv in settings.在设置中禁用 terminal.integrated.inheritEnv。 This was suggested by VSCode for me and it worked.这是 VSCode 为我建议的,并且有效。

I found a fix for this, install "pylint".我找到了解决方法,安装“pylint”。 I had a pop-up message in Visual Studio that asked me to download this extension.我在 Visual Studio 中有一条弹出消息,要求我下载此扩展程序。 I did and after that I was able to run my code!我做到了,之后我就可以运行我的代码了!

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.我不断遇到“在终端中运行 Python 文件”命令会导致语法错误而“在 Python 终端中运行选择/行”命令会出错但仍会显示结果的情况。 Irritating to say the least.至少可以说令人恼火。

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.注意:启用 Pylint 并没有解决我的问题,事实上,即使选择启用它,它仍然继续弹出。 These specific user/workspace/folder settings resolved that issue for me too.这些特定的用户/工作区/文件夹设置也为我解决了这个问题。

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",

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来完成。

I found the issue is generated by trailing spaces after the loop functions.我发现问题是由循环函数后的尾随空格产生的。 So what I do to alleviate it is add an empty print () statement at the very end of the script所以我所做的缓解它是在脚本的最后添加一个空的 print() 语句

我遇到了同样的问题,只需重新启动对我有用的 Vs-Code 即可!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Visual Studio Code 中运行 Anaconda python Hello World 的语法无效 - invalid syntax running Anaconda python Hello World in Visual Studio Code (Visual Studio代码上的Python)SyntaxError:语法无效 - (Python on Visual Studio Code) SyntaxError: invalid syntax mac 上的 Visual Studio Code 使用 python 错误“syntax error: invalid syntax”在“cerebro = bt.Cerebro()”行 - Visual Studio Code on mac using python Error "syntax error: invalid syntax" at the line 'cerebro = bt.Cerebro()' 在Visual Studio Code中运行Python代码? - Running Python code inside Visual Studio Code? SyntaxError:语法无效(从shell运行python代码时) - SyntaxError: invalid syntax (when running python code from shell) 运行python脚本时出错-语法无效 - Error when running a python script - invalid syntax 运行python 3代码时python 2语法错误 - python 2 syntax error when running python 3 code 我的代码在python shell中运行良好,但在我的Visual Studio代码上运行它时没有显示任何内容 - My code works fine inside the python shell, but not showing anything when running it on my Visual studio code IOError:[Errno 0]使用visual studio代码运行python代码时出错 - IOError: [Errno 0] Error when running python code using visual studio code 在 PythonAnywhere 上运行 Python 脚本时出现无效的语法错误 - Invalid Syntax Error when running Python script on PythonAnywhere
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM