Using the Windows Command Prompt

Both the Windows 95/98 and NT/2000 families come with a command prompt allowing users to run programs from a command-line interface. The Windows command prompt has a long and not-so-glorious history; it has grown from the first versions of MS-DOS and still uses the same basic syntax.

Running Python programs

For these discussions, let’s assume you have a file named C:\Scripts\hello.py, and this file consists of the single line:

print "Hello from Python"

A first attempt to run this program may surprise you:

C:\Scripts>python.exe hello.py
The name specified is not recognized as an
internal or external command, operable program or batch file.

C:\Scripts>

Although Python.exe has been installed, it hasn’t modified the system environment variable PATH: Windows doesn’t know how to find Python.exe. Windows NT does, however, know how to run .py files directly. Thus, if you ask the command prompt to execute hello.py, it will:

C:\Scripts>hello.py
Hello from Python

C:\Scripts>

Take this one step further and avoid the use of the .py extension, making a Python program appear like a .exe, .com, or .bat file. Windows NT supports this using the PATHEXT environment variable. You can view the current settings of this variable by entering the command:

C:\Scripts>echo %PATHEXT%
.exe;.bat;.cmd

C:\Scripts>

This is a list of the default extensions Windows searches for when a command is executed. You can add Python files to this list (and check the change was successful) by executing: ...

Get Python Programming On Win32 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.