Python Modules and Packages

A friendly goat mechanic in denim overalls takes a screwdriver from a red toolbox filled with tools, with text "Python packages and modules".

When you start coding in Python, your scripts can quickly grow larger than you expect. To stop your work becoming a messy jumble of functions, classes, and random bits of code, Python gives us modules and packages. These help keep your work organised, easy to reuse, and much easier to share with others.

What is a Module?

A module is simply a file containing Python code. Instead of cramming everything into one script, you can save parts of your program in separate .py files and then bring them back in when you need them.

For example, let’s create a file called greetings.py:

Now, in another file, you can import and use it:

Try it yourself:

  • Create your own module with a maths function (like squaring a number).
  • Import it into another script and test it.

What is a Package?

A package is just a folder that contains multiple modules. Imagine a recipe box full of recipe cards — the box is the package and the cards are the modules.

For example, make a folder called mytools with two files inside:

maths_tools.py

text_tools.py

Now, in your main script you can use:

Try it yourself:

  • Create your own package folder with at least two modules.
  • Use import and from ... import ... to access them.

Why They Matter

Modules and packages let you reuse code, keep your projects tidy, and borrow tools made by others. This is how Python libraries like pandas or Django are built — they are just big collections of well-structured packages.

Try it yourself:

  • Install a package (like requests) with pip install requests.
  • Write a small script that imports it and prints something from a webpage.

Main Topic

Python Modules and Packages

A sheep dressed as a chef happily pulls a recipe card labelled Python Modules from a wooden box of recipes on a table.

This tutorial introduces Python modules and packages, explaining their purpose, benefits, and everyday uses in keeping code organised, reusable, and efficient.

Other Tutorials in this Topic

A blue front door with a sign reading Python Module, beside a window showing cards labelled Version, Author, and Default Encoding.

Understanding __init__.py

This tutorial explains the purpose of `__init__.py` in Python packages, showing how it centralises variables, controls imports,…

A robot chef holds a steaming plate of food for a customer while clutching a recipe card to its chest labelled “Recipe”.

Private Functions in Python

This tutorial explains why private functions matter in Python, how to implement them in modules and packages,…