Running Python Programs
Whatever
tools you use to produce your Python application, you can see your
application as a set of Python source files. A
script is a file that you can run directly. A
module is a file that you can import (as covered
in Chapter 7) to provide functionality to other
files or to interactive sessions. A Python file can be both a module
and a script, exposing functionality when imported, but also suitable
for being run directly. A useful and widespread convention is that
Python files that are primarily meant to be imported as modules, when
run directly, should execute self-test operations. Testing is covered
in Chapter 17.
The Python interpreter automatically compiles Python source files as
needed. Python source files normally have extension
.py. Python saves the compiled bytecode file for
each module in the same directory as the module’s
source, with the same basename and extension
.pyc (or .pyo if Python is
run with option -O). Python does not save the
compiled bytecode form of a script when you run the script directly;
rather, Python recompiles the script each time you run it. Python
saves bytecode files only for modules you import. It automatically
rebuilds each module’s bytecode file whenever
necessary, for example when you edit the module’s
source. Eventually, for deployment, you may package Python modules
using tools covered in Chapter 26.
You can run Python code interactively, with the Python
interpreter or an IDE. Normally, however, you initiate ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access