[英]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
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.