How to Create a Python Package

Published: Tuesday 10th April 2012Last 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:

Create a directory and give it your package's name.

Put your classes in it.

Create a __init__.py file in the directory

That's all! In order to create a Python package, it is very easy. The __init__.py 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 Mammals.py inside the Animals directory and put the following code in it:

1

2

3

4

5

6

7

8

9

10

11

classMammals:

def__init__(self):

''' Constructor for this class. '''

# Create some member animals

self.members=['Tiger','Elephant','Wild Cat']

defprintMembers(self):

print('Printing members of the Mammals class')

formember inself.members:

print('\t%s '%member)

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 Birds.py inside the Animals directory and put the following code in it:

1

2

3

4

5

6

7

8

9

10

11

classBirds:

def__init__(self):

''' Constructor for this class. '''

# Create some member animals

self.members=['Sparrow','Robin','Duck']

defprintMembers(self):

print('Printing members of the Birds class')

formember inself.members:

print('\t%s '%member)

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

Step 3: Add the __init__.py File

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

1

2

fromMammals importMammals

fromBirds importBirds

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

The only think I would like to add is the usage of __all__ – list in the __init__.py. If __all__ is defined in __init__.py when you use "from my_package import *" only the modules/variables defined in __all__ will be loaded. For reference please check http://docs.python.org/tutorial/modules.html#impo…