“语法错误:无效语法"尝试在命令提示符中从 IDLE 运行 .py 脚本时 [英] "SyntaxError: Invalid Syntax" when trying to run a .py script from IDLE in Command Prompt

查看:72
本文介绍了“语法错误:无效语法"尝试在命令提示符中从 IDLE 运行 .py 脚本时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习python.我使用 IDLE 编辑器创建了一个简单的 .py 文件,并尝试从命令提示符运行它.但是,每次它不断给我语法错误:无效语法"留言.

I just started learning python. I created a simple .py file using the IDLE editor and I am trying to run it from the command prompt. However, every time it keeps giving me the "SyntaxError: Invalid Syntax" message.

这是使用记事本打开 .py 文件时的外观:

This is how the .py file looks when opened with notepad:

 Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32

 Type "copyright", "credits" or "license()" for more information.

 >>> import sys

 >>> print(sys.platform)

 win32

 >>> x="Spam!"

 >>> print(x*8)

 Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!

 >>> print(2**100)

 1267650600228229401496703205376

 >>> 

这就是我在 cmd 中输入的内容:

And this is what I type in the cmd:

 C:\code\script1.py

我们将不胜感激.

推荐答案

那不是 Python 程序,而是交互式(命令提示符)会话的日志.

That's not a Python program, it's the log of an interactive (command prompt) session.

相反,尝试在任何文本编辑器(例如记事本、记事本++)中输入以下内容,将其另存为 C:\code\script2.py,然后像您一样运行:

Instead, try entering the following in any text editor (e.g. notepad, notepad++), save it as C:\code\script2.py and then run it as you did:

import sys

print(sys.platform)

x="Spam!"
print(x*8)

print(2**100)

如果您想为此使用Idle,请单击[文件][新建]创建一个Python源代码文件,输入上面的内容,保存,然后像您一样运行.

If you want to use Idle for this, click [File][New] to create a Python source code file, type in the above, save it and then run it as you did.

Idle 是交互式开发环境 (IDE) 的一个示例.由于您是编程新手:IDE 往往会掩盖正在发生的事情,尽管 Idle 并不是一个严重的情况.因此,使用单独的编辑器并像您一样从命令行运行实际上是熟悉幕后情况的好方法.从长远来看,这将在很多方面获得回报.

Idle is and example of an Interactive Development Environment (IDE). Since you're new to programming: IDE's tend to obscure what's going on, although Idle isn't a severe case of this. So using a separate editor and running from the command line as you did is actually a good way to familiarize yourself with what's going on under the hood. This will pay off in many ways in the long run.

这篇关于“语法错误:无效语法"尝试在命令提示符中从 IDLE 运行 .py 脚本时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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