Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Modules and Packages

Open in Colab

Jupyter notebooks are great for some things, but they aren’t so great if we want to define a large library of functions to use.

You have probably noticed that we are able to import functions to use from numpy using the import keyword

import numpy
import numpy as np

You can do this with your own code by writing your own Python modules and packages.

Modules

A Python module is simply a .py file which contains definitions we would like to use. For instance, we can import the mymod module defined in mymod.py.

2

By default, the contents of the module will be imported into a namespace with the same name as the file (dropping the .py extension). We can re-name the namespace using the as keyword

2

We can also import specific functions or classes from a module using the from keyword

<mymod.myclass object at 0x7f38004a8a30>
2

If we want to import all functions:

2

you can see what is available in the mymod namespace using dir

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'myclass', 'plus1']

Modules vs. Scripts

What is the difference between a module and a script? Both are plain text files with a *.py extension.

Conceptually, a module contains functionality (e.g. functions, classes) that we would like to use and re-use, without needing to redefine in our interpreter every time. Typically, we use modules with an import statement. A script contains code that does something. This might include defining classes and functions, but usually also includes some sort of task such as analyzing data in a *.csv file, which it wouldn’t make sense to do in an import statement.

There isn’t really a clear cut line between modules and scripts - you might run a *.py file that was written for a module as a script, or import a *.py file that was written as a script.

If you want to have a file that can be used as both a module and a script, you can put the “scripting” part of the file in a block that begins with

if __name__ == '__main__':
    # scripting code here

An example can be found in mymod.py

__name__ is a global variable, which is set to __main__ when the file is executed from the command line, as in

python mymod.py

If the file is imported as a module, you see that __name__ is set to be the name of the file

'mymod'
'mymod'

Packages

Python packages are contained in a directory with an __init__.py file. These directories may contain muliple *.py files, as well as nested directories with __init__.py files (sub-packages). These directories might also contain other sorts of files such as documentation files or shared object libraries.

You can use the __init__.py file to automate import of modules inside a package, or modify namespaces.

An example can be found in the mypack directory.

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'functions', 'hello', 'more', 'mypack']

The following function is imported from mypack/mypack.py in __init__.py:

hello from mypack

cube is in mypack/functions.py

27

we can import a submodule from a package into a new namespace:

'mypack.functions'

square is also in mypack/functions.py

4

We can also have subpackages in packages (subdirectories of our directory).

plus2 is defined in mypack/subpackage/functions.py

5

Conventions

By convention, Python packages and modules should have short lowercase names. See here for reference.