名称错误:名称“python”未定义。

15

我在Windows命令行中遇到了这个错误,进行了广泛的搜索,但没有找到完美的答案。请看下面的错误并帮助解决。

python
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
>>>

提前致谢,


你是如何得到这个回溯信息的?你输入了什么?我猜你尝试执行 python 命令来启动解释器,然后再次输入 python,但它被解释为变量名。 - Rushy Panchal
你没有在单词“python”之前包含“>>>”,对吗? - Elazar
2个回答

47

看起来你试图通过运行命令python启动Python解释器。

但是解释器已经启动了。它将python解释为变量名,并且该名称未定义。

请尝试以下命令,您应该会看到您的Python安装按预期工作:

print("Hello world!")

当我输入这段代码时,它可以正常工作,但是当我只输入“python”时,它会显示错误,提示名称错误:python未定义。这代表什么意思?>>> print("hello world") hello world
- user13050

21
当您在运行Windows命令提示符并键入python时,它会启动Python解释器。
再次键入它会尝试将python解释为一个不存在的变量,因此不起作用:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\USER>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> python
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
>>> print("interpreter has started")
interpreter has started
>>> quit() # leave the interpreter, and go back to the command line

C:\Users\USER>

如果您不是从命令行执行此操作,而是直接运行Python解释器(python.exe或IDLE的shell),那么您不在Windows命令行中,并且python被解释为变量,而您尚未定义。


可能他根本没有运行命令行,而是直接运行了Python解释器。 - Elazar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接