VS Code运行Python脚本报错SyntaxError: invalid syntax求助
解决VS Code中运行Python脚本的SyntaxError问题
问题描述
在VS Code中尝试运行路径为c:/Users/Andrew/Documents/GitHub/Digital-solutions-assignment/Movie booking system.py的Python脚本时,出现以下语法错误:
SyntaxError: invalid syntax
& C:/Users/Andrew/AppData/Local/Microsoft/WindowsApps/python3.10.exe "c:/Users/Andrew/Documents/GitHub/Digital-solutions-assignment/Movie booking system.py"
File "", line 1
& C:/Users/Andrew/AppData/Local/Microsoft/WindowsApps/python3.10.exe "c:/Users/Andrew/Documents/GitHub/Digital-solutions-assignment/Movie booking system.py"
^
脚本的导入及数据库连接部分代码如下:
#Imports and Database connection-------------------------------------- import sqlite3 from tkinter import Menu from unittest import result #importing the code for using sql lite conn = sqlite3.connect('Moviebookingsystem.db') #this string will depend on where the database is compared to this file cur = conn.cursor() #create cursor to database
问题原因
你在Python交互解释器(>>>提示符)中输入了终端命令,而Python交互环境只能执行Python代码,无法识别终端的脚本启动指令,这才引发了语法错误。
解决方法
方法1:使用VS Code内置运行功能
- 打开目标脚本文件
Movie booking system.py - 点击右上角的▶️运行按钮,或按下
F5键,VS Code会自动调用Python解释器运行当前脚本
方法2:在VS Code终端中执行命令
- 打开VS Code终端(快捷键
Ctrl+) - 在终端中直接输入命令(不要带
>>>提示符):
若已进入脚本所在目录,可简化为:python3.10.exe "c:/Users/Andrew/Documents/GitHub/Digital-solutions-assignment/Movie booking system.py"python "Movie booking system.py"
额外修复:脚本自身的语法问题
从提供的代码片段看,存在注释换行导致的语法隐患:
conn = sqlite3.connect('Moviebookingsystem.db') #this string will depend on where the database is compared to this file
第二行的where会被当作Python代码执行,需将注释合并为一行,或改用多行注释:
# 合并注释为一行 conn = sqlite3.connect('Moviebookingsystem.db') # 这个路径取决于数据库文件与当前脚本的相对位置 # 或使用多行注释 """ 这个路径取决于数据库文件与当前脚本的相对位置 """ conn = sqlite3.connect('Moviebookingsystem.db')
内容的提问来源于stack exchange,提问作者P4RK3R
相关产品推荐
相关产品推荐

