how to run .py files in command prompt (Window

2019-09-09 21:08发布

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

print "Hello"

with various methods, including IDLE (Python GUI), Python (Command Line) and finally, command prompt, but to no avail.

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:

>>> 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?

标签: python cmd
3条回答
冷血范
2楼-- · 2019-09-09 21:52

You should use the plain CMD (command prompt) console, not the python interpreter and then type:

python new.py

Hope that works!

查看更多
Ridiculous、
3楼-- · 2019-09-09 21:52

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. Do this enables you to run the python command from the command line as e.g. python new.py, and also if new.py is in the current directory you can just type new.py to run it :-)

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-09-09 21:55

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.

查看更多
登录 后发表回答