NameError:未定义名称“python”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16857105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:50:01  来源:igfitidea点击:

NameError: name 'python' is not defined

pythonnameerror

提问by user13050

Am encountering this error in Windows Command line,done a wide search but could not get a perfect answer.Please find the error below and help in solving.

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

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

Thanks in Advance,

提前致谢,

回答by Mark Byers

It looks like you are trying to start the Python interpreter by running the command python.

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

However the interpreter is already started. It is interpreting pythonas a name of a variable, and that name is not defined.

然而,解释器已经启动。它解释python为变量的名称,而该名称未定义。

Try this instead and you should hopefully see that your Python installation is working as expected:

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

print("Hello world!")

回答by Rushy Panchal

When you run the Windows Command Prompt, and type in python, it starts the Python interpreter.

当您运行 Windows 命令提示符并输入 时python,它会启动 Python 解释器。

Typing it again tries to interpret pythonas a variable, which doesn't exist and thus won't work:

再次输入它会尝试将其解释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>

If you're not doing this from the command line, and instead running the Python interpreter (python.exe or IDLE's shell) directly, you are not in the Windows Command Line, and pythonis interpreted as a variable, which you have not defined.

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