Agentic AI

Modules and packages

Import code from the standard library, from installed packages and from your own files, and run a file as a script.

Concept + code Python 3.12

A module is a Python file you can import; a package is a folder of modules. Every library you install, from NumPy to LangChain, is a package, imported in one of the ways below.

Import a module

Notebook cellPython
import math
print(math.sqrt(16))

from math import sqrt, pi
print(sqrt(25), pi)
OutputReal output
4.0
5.0 3.141592653589793

import math gives you the module; you reach its contents as math.sqrt. from math import sqrt, pi brings those two names in directly.

Give it a short name

Notebook cellPython
import numpy as np
np.array([1, 2, 3, 4])
OutputReal output
array([1, 2, 3, 4])

as renames the import. Some short names are near-universal: np for NumPy, pd for pandas. NumPy is a third-party package, so it has to be installed first (lesson 1.1).

Import everything

Notebook cellPython
from math import *
print(sqrt(16), pi)
OutputReal output
4.0 3.141592653589793

* brings in every public name of the module. It works, but readers can no longer see where a name comes from, and an imported name can silently replace one of yours. Prefer explicit imports.

Your own package

Build this folder next to your notebook:

my-project/
├── notebook.ipynb
├── test.py
└── package/
    ├── __init__.py
    ├── maths.py
    └── subpackages/
        ├── __init__.py
        └── mult.py
package/__init__.pyPython
"""Marks this folder as a package. Runs when the package is first imported."""
package/maths.pyPython
def addition(a, b):
    return a + b


def subtraction(a, b):
    return a - b
package/subpackages/__init__.pyPython
# empty: marks subpackages as a package too
package/subpackages/mult.pyPython
def multiply(a, b):
    return a * b
Notebook cellPython
from package.maths import addition
print(addition(2, 3))

from package import maths
print(maths.subtraction(4, 3))

from package.subpackages.mult import multiply
print(multiply(4, 5))
OutputReal output
5
1
20

Dots follow the folders: package.subpackages.mult is package/subpackages/mult.py. Python finds package because it searches the folder the notebook runs in, which in VS Code is the notebook's own folder VERIFY. __init__.py makes a folder a regular package and runs on its first import; it can stay empty.

Run a file as a script

test.pyPython
from package.maths import *
from package.subpackages.mult import multiply

print(addition(2, 3))
print(subtraction(4, 3))
print(multiply(4, 5))
Terminalzsh
python test.py
OutputReal output
5
1
20

Code that runs only as a script

Every module has a name in __name__. It is "__main__" when the file is run directly, and the module's own name when it is imported:

greet.pyPython
def greet(name):
    return f"Hello, {name}!"


if __name__ == "__main__":
    print(greet("Alice"))
Terminalzsh
python greet.py
OutputReal output
Hello, Alice!
Notebook cellPython
from greet import greet
print(greet("Bob"))
OutputReal output
Hello, Bob!

Run as a script, the file prints its greeting; imported, it only provides greet(), and the block under the if stays silent. Scripts that start a web server or an agent end with this block.