Last Updated: Thursday 9th May 2013

When you've got a large number of Python classes (or "modules"), you'll want to organize them into packages. When the number of modules (simply stated, a module might be just a file containing some classes) in any project grows significantly, it is wiser to organize them into packages – that is, placing functionally similar modules/classes in the same directory. This article will show you how to create a Python package.

Steps to Create a Python Package

Working with Python packages is really simple. All you need to do is:

  1. Create a directory and give it your package's name.
  2. Put your classes in it.
  3. Create a file in the directory

That's all! In order to create a Python package, it is very easy. The file is necessary because with this file, Python will know that this directory is a Python package directory other than an ordinary directory (or folder – whatever you want to call it). Anyway, it is in this file where we'll write some import statements to import classes from our brand new package.

Example On How to Create a Python Package

In this tutorial, we will create an Animals package – which simply contains two module files named Mammals and Birds, containing the Mammals and Birds classes, respectively.

Step 1: Create the Package Directory

So, first we create a directory named Animals.

Step 2: Add Classes

Now, we create the two classes for our package. First, create a file named inside the Animals directory and put the following code in it:

The code is pretty much self-explanatory! The class has a property named members – which is a list of some mammals we might be interested in. It also has a method named printMembers which simply prints the list of mammals of this class! Remember, when you create a Python package, all classes must be capable of being imported, and won't be executed directly.

Next we create another class named Birds. Create a file named inside the Animals directory and put the following code in it:

This code is similar to the code we presented for the Mammals class.

Step 3: Add the File

Finally, we create a file named inside the Animals directory and put the following code in it:

That's it! That's all there is to it when you create a Python package. For testing, we create a simple file named in the same directory where the Animals directory is located. We place the following code in the file:

To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.

  • Very nice and simple.

    The only think I would like to add is the usage of __all__ – list in the If __all__ is defined in when you use "from my_package import *" only the modules/variables defined in __all__ will be loaded. For reference please check

  • Vijay Santore

    Very Simple and well explanatory

  • ali

    simple and very useful :X

  • Brian Tom

    I’d like to note that according to PEP0008(The official style guide for python) that the package names should be all lowercase and with underscores.

  • Aashish Shivhare

    Simple and good explanation. Thanks

    • karri

      dj prem chaithanya

  • Rishikesh Agrawani

    Excellent article.

    • inturi

      neyamma puku ra lavda ga

    • ginne

      pilla lanjodaka ginne na madda

  • Manikandan V

    Can we use this method to create Python package for windows Platform?