简体   繁体   English

如何在命令提示符下运行 .py 文件 (Windows 7)

[英]how to run .py files in command prompt (Windows 7)

I am facing an extremly tiresome and difficult problem for me, one for which i was anable to find any solution for quite some time, my google searching skills failing me miserably.我面临着一个对我来说极其烦人和困难的问题,我在很长一段时间内都能够找到任何解决方案,我的谷歌搜索技能让我悲惨地失败了。

I have tried to open a.py file with a single line of code in it我试图打开一个只有一行代码的.py文件

print "Hello"打印“你好”

with various methods, including IDLE (Python GUI), Python (Command Line) and finally, command prompt, but to no avail.使用各种方法,包括 IDLE(Python GUI)、Python(命令行),最后是命令提示符,但无济于事。

I was eventually able to set the PATH variable in Windows and access the python interpreter through the cmd, but when typing the name of the file I keep getting the same error message:我最终能够在 Windows 中设置 PATH 变量并通过 cmd 访问 python 解释器,但是在键入文件名时,我不断收到相同的错误消息:

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

What could I do to solve it, and finally be able to run my.py file?我该怎么做才能解决它,并最终能够运行 my.py 文件?

You should use the plain CMD (command prompt) console, not the python interpreter and then type: 您应该使用普通的CMD(命令提示符)控制台,而不是python解释器,然后键入:

python new.py

Hope that works! 希望能成功!

In the command prompt, 在命令提示字元中,

python filepath.py

If there's an error that python is an unrecognized command, you should add the python install directory to your system path first (in environment variables) and then re-open the command prompt. 如果出现python是无法识别的命令的错误,则应首先将python安装目录添加到系统路径中(在环境变量中),然后重新打开命令提示符。

When you run the Windows python installer, at the point you are asked what to install, usually not visible at the bottom of the list of features is an option to modify the system path. 运行Windows python安装程序时,系统会提示您安装什么(通常在功能列表底部不可见)是修改系统路径的选项。 Do this enables you to run the python command from the command line as eg python new.py, and also if new.py is in the current directory you can just type new.py to run it :-) 这样做可以使您从命令行运行python命令,例如python new.py,并且如果new.py在当前目录中,则只需键入new.py即可运行它:-)

Write this in the file,把这个写在文件中,

python filename.py

Just change the filename to the file name.只需将filename名更改为文件名即可。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM