Running a Python Program

A text file consisting of Python code is called a program, or a script, or a module. There is little distinction between the three terms — generally a script is smaller than a program, and a file designed to be imported (rather than executed directly) is called a module. Normally, you name Python code files with a .py extension.

To run a program named spam.py, type the following at a command prompt:

python spam.py

In Windows, you can run a program by double-clicking it. (If the file association for the .py extension is not set up at installation time, you can configure it by right-clicking the script, choosing "Open With..." and then choosing python.exe.)

In UNIX, you can run a script directly by using the "pound-bang hack." Add this line at the top of the Python script (replacing the path with the path to env if it's different on your system):

#!/usr/bin/python

Then make the file executable (by running chmod +x <filename>), and you can run it directly.

Was this article helpful?

0 0

Post a comment