简体   繁体   English

从CMD Windows 10运行.py文件时出现NameError

[英]NameError when running .py file from CMD Windows 10

I am trying to run a .py file created in a text editor from the CMD line in Windows 10. Here is my very simple code: 我正在尝试运行Windows 10中CMD行中在文本编辑器中创建的.py文件。这是我非常简单的代码:

def main():
    print 'It works!'

if __name__ == '__main__':
    main()

When I run from CMD line, which is already in python 2.7 mode, i type 当我从已经处于python 2.7模式的CMD行运行时,我键入

pytest.py

which is the name of the file. 这是文件名。 However, now the CMD line says: 但是,现在CMD行显示:

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

CMD截图

CMD截屏2

You cannot run the .py file from the Python interpreter (starting with >>> ) 您无法从Python解释器运行.py文件(以>>>开头)

So, you need to see C:\\Users\\Eric> python pytest.py to run python on your file. 因此,您需要查看C:\\Users\\Eric> python pytest.py才能在文件上运行python

Or , you can run only python , then you must import the file. 或者 ,您只能运行python ,然后必须import文件。

>>> import pytest
>>> pytest.main()

Both cases assume the CMD is at the same directory as your file. 两种情况都假定CMD与文件位于同一目录。 If not, you must cd to that proper directory first, or use 如果不是,则必须先cd到该正确的目录,或使用

C:\Users\Eric> python C:\Users\Eric\full\path\to\pytest.py

When you start a terminal in windows via CMD, you are in the Windows Command Line. 通过CMD在Windows中启动终端时,您位于Windows命令行中。 Here you can run your python code by entering 在这里,您可以输入以下命令来运行python代码

python yourpythoncode.py

Or you can choose to start the python interpreter by entering just : 或者,您可以选择只输入来启动python解释器:

python

In the interpreter you can run your python program by importing it 在解释器中,您可以通过导入来运行python程序

import yourpythoncode

If yourpythoncode has a line like 如果你的python代码有一行

if ___name___ = ___main___:
    main()

then it is protected from autorunning the code. 那么就可以保护它免于运行代码。 So to run your code your still need to call it explicit by entering : 因此,要运行您的代码,您仍然需要通过输入来显式调用它:

main()

Either make the file executable or supply it to python program to run it 将文件设为可执行文件或将其提供给python程序以运行它

python pytest.py

If you are running the file from within the python interpreter, then you need to exit that using Ctrl + Z and run it from the command line the way I mentioned above. 如果您是从python解释器中运行文件,则需要使用Ctrl + Z退出该文件,然后按照我上面提到的方式从命令行运行它。

Note: You will need to change to the directory where pytest.py is located in for the above command to work; 注意:您需要切换到pytest.py所在的目录,以上命令才能pytest.py or you need to supply the path to the file. 或者您需要提供文件的路径。 For example, from your pictures, you are in the root directory ie C:\\Users\\Eric ; 例如,从您的图片中,您位于根目录下,即C:\\Users\\Eric if you open file explorer on windows and navigate to where your file is located, you can right click the file and view properties and this should show you the location. 如果您在Windows上打开文件资源管理器并导航到文件所在的位置,则可以右键单击该文件并查看属性,这将向您显示位置。 Then in your command prompt, you need to type cd C:\\location\\you\\just\\copied\\ then after that you should be able to run the file using the python command above 然后在命令提示符下,键入cd C:\\location\\you\\just\\copied\\然后应该可以使用上面的python命令运行文件

暂无
暂无

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

相关问题 只需键入 python nameofthescript.py 即可在 Windows 10 cmd 上运行 python 脚本 - Running python scripts on windows 10 cmd just by typing python nameofthescript.py 来自 cmd 的 Running.py 文件打开 python 修改设置 - Running .py file from cmd opens the python modify setup 从 cmd 运行一个包含 matplotlib plot 的 py 文件 - Running a py file from cmd that contain matplotlib plot 从 cmd 运行时出现 Python setup.py 错误 - Python setup.py error when running from cmd 从 cmd 运行时,“模块”对象没有属性“py” - 'module' object has no attribute 'py' when running from cmd 运行 .py 脚本的 CMD 窗口不写入 txt 文件,但在 pycharm 中运行确实写入文件 - CMD windows running .py scrip not writing to txt file, but running in pycharm does write to the file 为什么 matplotlib.pyplot 在使用 jupyter notebook 时工作正常,但从 CMD 中的 a.py 文件运行时却不起作用? - why does matplotlib.pyplot works fine when using jupyter notebook but it does not work when running from a .py file in CMD? 我无法在Windows 10上的cmd或Kali Linux的终端中运行.py文件 - I cannot run a .py file in cmd on windows 10, or in the terminal in kali linux 从 cmd 运行 python py 文件但得到 ImportError。 从 conda 提示符运行正在运行 - Running python py file from cmd but getting ImportError. Running from conda prompt is working 从cmd运行py脚本的问题 - Problems with running py scripts from cmd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM