简体   繁体   中英

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

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?

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

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.

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 eg python new.py, and also if new.py is in the current directory you can just type new.py to run it :-)

Write this in the file,

python filename.py

Just change the filename to the file name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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