11

我在 Windows 命令行中遇到此错误,进行了广泛的搜索,但无法得到完美的答案。请在下面找到错误并帮助解决。

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

提前致谢,

4

2 回答 2

41

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

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

试试这个,你应该希望看到你的 Python 安装按预期工作:

print("Hello world!")
于 2013-05-31T12:21:23.783 回答
17

当您运行 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解释为您尚未定义的变量。

于 2013-05-31T12:23:09.283 回答