Python Packages Comprehensive Guide

1. Python Packages

In today’s article, we will discuss packages in Python, Python Packages Structure, list, modules, import file and much more.

2. What are Python Packages?

Python Packages

Python Packages

In our computer systems, we store our files in organized hierarchies. We don’t store them all in one location. Likewise, when our programs grow, we divide it into packages. In real-life projects, programs are much larger than what we deal with in our journey learning Python. A package lets us hold similar modules in one place.

Like a directory may contain subdirectories and files, a package may contain subpackages and modules. We have been using modules a lot in the previous lessons. Remember math, os, and collections? Those were all modules that ship with Python officially. We will discuss the difference between a module and a package in our next lesson. But for now, let’s dive into the world of packages.

3. The Structure

As we discussed, a package may hold other Python packages, and modules. But what distinguishes a package from a regular directory? Well, a Python package must have an file in the directory. You may leave it empty, or you may store initialization code in it. But if your directory does not have an file, it isn’t a package; it is just a directory with a bunch of Python scripts. Leaving empty is indeed good practice.

Take a look at the following structure for a game:

Python Packages Module Structure

Python Packages Module Structure

Here, the root package is Game. It has subpackages Sound, Image, and Level, and file Sound further has modules load, play, and pause, apart from file Image has modules open, change, and close, apart from Finally, Level has modules start, load, and over, apart from

4. Importing Modules from Python Packages

A package may contain several modules. To import one of these into your program, you must use the dot operator(.)

In the above example, if you want to import the load module from subpackage Sound, we type the following at the top of our Python file:

import Game.Sound.load

Note that we don’t type the extension, because that isn’t what we refer to the module as. The subpackage Level has a module named load too, but there is no clash here. This is because we refer to the module by its fully qualified name.

To escape having to type so much every time we needed to use the module, we could also import it under an alias:

import Game.Sound.load as loadgame

(If you’re working the interpreter, you may also do the following:


This works equally fine.)

Alternatively, you could do:

from Game.Sound import load

Now, if the Sound subpackage has a function volume_up(), we call it this way:


If we imported this way:

from Game.Sound.load import volume_up() as volup

We could call the function simply, without needing to use a full qualifier:


But this isn’t recommended, as this may cause names in a namespace to clash.

5. Further Notes

When you import a package, only the modules directly under it are imported. An import does not import the subpackages.

>>> import one
>>> one.two
Traceback (most recent call last):
 File "<pyshell#488>", line 1, in <module>

AttributeError: module ‘one’ has no attribute ‘two’

Also note that if you want to check where your packages are being created, your path will look something like this:


6. Creating Your Own Python Packages

Now, on to the most interesting part. Like we said, Python packages are nothing but a dictionary with subpackages and modules, and an file.

In our example, this is the hierarchy we create:

Python Packages

Python Packages

This is what we have in

def check():

a=int(input(‘Enter a number’))

if a%2==0: print(“Even”)

else: print(“Odd”)

Also, we keep each empty.

Now, we import and use it this way:

>>> from one.two.evenodd import check as check
>>> check()

Enter a number7


>>> check()

Enter a number0


7. Conclusion

In this Python Packages tutorial, we discussed packages, and how to create them. Apart from that the Python Package Index(PyPI) provides us a lot of packages to help us with our projects. We talked about this in our Introduction to Python. Next, we will discuss how these differ from modules. Till then, keep practicing.