How do I run a Python program in Windows?
I have downloaded the Python package from the website. It is in my C drive, in a directory named Python27. In the command prompt, I type the following:
cd C:\Python27
pythonThen I get Python 2.7.4. and the opening message. My Python file, called first.py, is in the desktop, and I don't know how to access it. I've tried the following..
python first.py
python first.py
python cd Desktop\first.py
python cd Desktop\"first.py"What is the correct command?
45 Answers
You have to add python to your path, then you can open a command line in the directory of your file and type:
python first.py 3 If your program does not need the console (e.g. automation scripts or GUI programs), then double click on the script file to execute it.
Otherwise, in modern Windows (Vista, Seven, 8), you folder is located under \Users. Let's say the script script.py is the Desktop folder of user Foo, so it would be:
python c:\Users\Foo\Desktop\script.py 2 Go like this:
- open a console
cd Desktop
c:\Python27\python first.py
Best to add C:\Python27 to the system PATH variable though.
You can also do this in command prompt:
start C:\Users\your_username_here\Desktop\first.pyYou can even do this in python! Here is how:
import os
os.system("start "+yourPathHere)A very simple program would be:
import os
while True: userInput=str(input("Enter file/app path: ")) os.system("start "+userInput) print ("") # - just for the looks -The more you know :p
Make sure you set up the path like this by adding this in the Environmental Variables: %path% on your settings.C:\WINDOWS\system32;C:\WINDOWS;C:\Python27