从 Visual Studio Code 内部运行 python 时出现无效语法错误 [英] Invalid Syntax error when running python from inside Visual Studio Code

查看:141
本文介绍了从 Visual Studio Code 内部运行 python 时出现无效语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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)

当我在 Visual Studio Code 中使用 Python 3.7 打开这个文件时,我收到以下错误:

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

/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

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

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

但是在我的终端中使用 python3 test.py 运行文件效果很好.

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

有谁知道在 VS Code 中运行它时会出现什么问题?

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

推荐答案

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

Looks like this is a bug in VS Code.

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

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终端中运行选择/行"保存它然后运行在终端中运行Python文件"它不起作用.所以这似乎是一个与 VS Code 相关的问题.

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.

这篇关于从 Visual Studio Code 内部运行 python 时出现无效语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆