简体   繁体   English

尝试在 cmd (Windows 10) 中运行 python 脚本时出现语法错误

[英]Syntax error when try to run python scripts in cmd (Windows 10)

I just start learning python programming.我刚开始学习python编程。 I wrote a simple program in python 3.8.2:我用python 3.8.2写了一个简单的程序:

x = "Hi world"
print (x)

Code was saved in a python file named 'file.py'.代码保存在名为“file.py”的 python 文件中。 It ran correctly in python.它在python中正确运行。 The problem is when try to run the same chunk of code in the command line of windows 10.问题是当尝试在 Windows 10 的命令行中运行相同的代码块时。

After cmd was opened, i set the path at the directory where the file is saved, in this case, my desktop.打开cmd后,我将路径设置在保存文件的目录中,在这种情况下,我的桌面。 Then i tried to run the next command:然后我尝试运行下一个命令:

C:\Users\Ben\Desktop>python file.py

The program was not work.该程序不起作用。 Windows shows me the following message: Windows 向我显示以下消息:

File "file.py", line 1 Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32 ^ SyntaxError: invalid syntax文件“file.py”,第 1 行 Python 3.8.2(tags/v3.8.2:7b3ab59,2020 年 2 月 25 日,22:45:29)[MSC v.1916 32 位(英特尔)] on win32 ^ SyntaxError: invalid syntax

I will appreciate all your help.我将感谢您的所有帮助。

If file.py just contained:如果 file.py 刚刚包含:

x = "Hi world"
print (x)

then that is fine but you have:那很好,但你有:

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)]

as your first line.作为你的第一行。

This is most likely as it is imported from the default python IDLE这很可能是因为它是从默认的 python IDLE 导入的

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM