Skip to main content
As programs grow larger, you’ll want to split them into multiple files for easier maintenance. Python’s module system lets you organize and reuse code effectively.

What is a Module?

A module is a file containing Python definitions and statements. The filename is the module name with the suffix .py added.

Creating a Module

Create a file called fibo.py:
fibo.py

Using a Module

Import and use the module:
Assign functions to local names:

More on Modules

Modules can contain executable statements as well as function definitions. These statements initialize the module and run only the first time the module is imported.

Import Variants

Import specific names:
Import all names (not recommended):
Using from module import * is generally frowned upon as it can introduce an unknown set of names into your namespace and hide definitions you’ve already made.
Import with an alias:

Executing Modules as Scripts

Add this code to make your module executable:
fibo.py
Now you can run it:
When imported, the code doesn’t run:
This pattern is commonly used to provide a convenient interface for testing purposes or to make modules usable both as scripts and importable libraries.

The Module Search Path

When importing a module named spam, Python searches:
  1. Built-in modules (listed in sys.builtin_module_names)
  2. Directories in sys.path, which includes:
    • The directory containing the input script (or current directory)
    • PYTHONPATH (environment variable)
    • Installation-dependent defaults (including site-packages)
You can modify sys.path at runtime:

Compiled Python Files

Python caches compiled modules in the __pycache__ directory as module.{version}.pyc files to speed up loading. Key points:
  • Python checks if the source is newer than the compiled version
  • .pyc files are platform-independent
  • Programs don’t run faster from .pyc files; they just load faster

Standard Modules

Python comes with a library of standard modules. For example, the sys module:
Modify the module search path:

The dir() Function

Find out which names a module defines:
Without arguments, list currently defined names:
List built-in functions and variables:

Packages

Packages are a way of structuring Python’s module namespace using “dotted module names”.

Package Structure Example

Importing from Packages

Import individual modules:
Use the full name:
Import the module directly:
Import a specific function:

Importing * From a Package

To control what from package import * imports, define __all__ in __init__.py:
sound/effects/__init__.py
Now:
If __all__ is not defined, from sound.effects import * does not import all submodules. It only ensures the package is imported and then imports any names defined in __init__.py.

Intra-package References

Use relative imports within packages:
Explanation:
  • . refers to the current package
  • .. refers to the parent package
Relative imports are based on the current module’s name. The main module must always use absolute imports.

Packages in Multiple Directories

Packages support the __path__ attribute, which can be modified to extend the set of modules found in a package. This feature is rarely needed but can be used to extend packages.

Next Steps

Now that you understand modules and packages, learn about Input and Output including file handling and data serialization.