Execute a Python script on Microsoft Windows

There are three ways you can execute your Python script on Microsoft Windows.

1. In IDLE

As you already know, you can execute your Python script in IDLE by pulling down
Run → Run Module

2. In Windows Explorer

Insert the following statement immediately before each call in your script to the sys.exit function.

input("Press enter to dismiss this window.")

Double-click on the icon for C:\Users\Myname\python\inout.py and the Python script will run. This is because we established a “file association” when we installed Python: a file whose name ends with .py will automatically be executed by C:\windows\py.exe. To display this association, type the assoc and ftype commands we saw here.

3. In the Command Prompt window

Let’s append the name of our C:\Users\Myname\python directory to our PATH environment variable. This variable contains a list of directory names, separated by semicolons. Type your name instead of Myname.

On my Windows 7 Home Premium, I went to
Control Panel → System and Security → System → Advanced System Settings
Do you want to allow the following program to make changes to this computer?
System Properties → Advanced → Environment Variables…

Environment Variables → System variables
Path
Edit…

Edit System Variable
Variable name: Path
Variable value: I appended ;C:\Users\Myname\python

When you’re done editing, press OK twice. Then open a new Command Prompt window. You can now run the script inout.py without having to tell the computer where to find inout.py, because this script is in one of the directories (namely, C:\Users\Myname\python) in your PATH.

path
PATH=now the PATH includes the additional directory

inout.py
How old are you?