Back to articles list Articles
5 minutes read

Python Module and a Python Package: Key Differences

One of Python’s many advantages is its packages – or are they Python modules? Discover the difference between Python packages and Python modules and learn why both are important in this article.

Python is an open-source language, meaning its existing and new developments are freely available to everyone. Anyone can write some code and then make it available for others to download, install, and use. You might find these programs referred to as Python modules or Python packages. But what is the difference between these two terms? We’ll explore this question in this article.

As a beginner, it’s common to hear terms you don’t understand – like Python package vs. Python module. We have two articles which describe 20 of the most important terms you might hear. These will give you a good foundation, so check out Python Terms Every Beginner Should Know Part 1 and Part 2.

If you’re looking for some hands-on learning material, consider taking our Learn Programming with Python track. It contains plenty of interactive coding challenges to get you going.

Many Modules Make a Package

Modules and packages both make code reusable among many people. You can write a module to calculate the area of various shapes. And you can give this module to your friends so they can use it without having to rewrite all the code. This saves time and effort and helps avoid mistakes.

A Python module is any single python file. The name of the module is the same as the name of the file (without the .py extension). If you have some functions to calculate the area of shapes, these could be written in the file areas.py, which forms a module called areas.

A Python package is a collection of Python modules in a directory. If we wanted to add another module for calculating the volumes of shapes, we could put this code into volumes.py, save the file in the same directory as areas.py, and create an __init__.py file. This would then be a Python package.

By the way, the __init__.py file distinguishes a Python package from a normal directory that just happens to contain several python files.

Finally, you may have also heard the term Python library. A library is a collection of Python packages.

Importing Python Packages

There are Python packages for almost everything. If you’re into data analysis, you should know about the Python packages NumPy, pandas, scikit-learn, and Matplotlib (to name a few). We’ve discussed some of these in our article on the Most Popular Python Packages in 2021, but they’ll never get old. For the aspiring data scientists out there, we have another article on the Top 15 Python Libraries for Data Science. Or if web scraping is more your thing, you might be familiar with packages like Requests, BeautifulSoup, or Selenium.

Let’s take a closer look at Matplotlib, which is useful for visualising data. The simplest way to import this library is as follows:

>>> import matplotlib

Every time you want to use a module in matplotlib, you have to start by writing matplotlib.<module_name>. To save yourself some typing, you can do this:

>>> import matplotlib as mpl

To use the pyplot module (which helps you develop data visualizations), start by typing mpl.pyplot. You’ll mostly want access to a specific function within the module. To plot a bar chart, you can write …

>>> mpl.pyplot.bar(x, height)

where x and height are the two required arguments. If you plan on plotting many bar charts, you can import the module directly; this makes access to the bar() function easier:

>>> import matplotlib.pyplot as plt
>>> plt.bar(x, height)

To see some concrete examples using Matplotlib, check out our articles How to Plot a Running Average in Python Using Matplotlib and How to Visualize Sound in Python.

Create Your Own Python Module and Package

A good exercise is to create your own Python module and package. Start by creating a new directory called shapes. Then create an __init__.py file and leave it empty for now.  We’ll come back to this weird file later.

The first module will be areas.py. Create this file and write the following function in there:

def area_of_circle(r):
    pi = 3.14
    area = pi * (r**2)
    return area

The next module will be volumes.py. Again, create this file with the following function:

def volume_of_sphere(r):
    pi = 3.14
    volume = (4./3.) * pi * (r**3)
    return volume

To use the volume_of_sphere() function in another piece of code, just do the following:

>>> import shapes.volumes as sv
>>> sv.volume_of_sphere(2)
33.49333333333333

If you want some more details on creating your own Python modules, check out our article How to Write a Custom Module in Python.

What About __init__.py?

In the last example, we imported the volumes module directly. Try importing this package and then calling the function from the module:

>>> import shapes
>>> shapes.volumes.volume_of_sphere(2)

You’ll get the following error message:

AttributeError: module 'shapes' has no attribute 'volumes'

This problem can be addressed by importing our modules in __init__.py. We won’t be going into the details of this file, but in practice, it is run when importing the package. Open __init__.py and add the following line:

import shapes.volumes

Save the changes, then open a new Python console and try to import the package again:

>>> import shapes
>>> shapes.volumes.volume_of_sphere(2)
33.49333333333333

Now we can import either the whole package or the individual modules (like we did above with Matplotlib). And that’s it! You’ve created your own Python module and package!

Now That You Understand Python Modules and Packages …

Where do you go from here?

We discussed the difference between Python modules and packages and showed some examples. Taking full advantage of Python packages will make your programming easier and more reliable, which is why we often discuss the best Python packages available for different use cases. For the beginners out there, Python Libraries Every Programming Beginner Should Know has a useful list of libraries. Our article The Most Popular Python Libraries contains some familiar faces, plus one we haven’t mentioned so far.

For  more details and examples of the material we covered here, take a look at the article Difference Between Python Modules, Packages, Libraries, and Frameworks. Taking the time to learn Python is worth it, as we discuss in the Benefits of Learning Python. Happy coding!