Home > Articles > Programming > Python

An Introduction to Object-Oriented Concepts in Python, Part 2

  • Print
  • + Share This
Programming expert Jesse Smith shows how to get started creating Python programs using modules and packages.
Like this article? We recommend

In this article, I'll continue showing you how to use Python from an object-oriented (OO) point of view. If you're unfamiliar with object-oriented concepts such as inheritance, encapsulation, and polymorphism, read my article "A Primer on Object-Oriented Concepts" before continuing with this article.

This article builds on my earlier article "An Introduction to Object-Oriented Concepts in Python, Part 1." If you haven't read that article already, please read it before reading this article. The concepts in this article will take a closer look at Python modules and packages.

If you've been following my Python series, you know how classes in Python work. But so far I haven't addressed how Python classes can be used in modules and packages. As in other programming languages, in Python you need to import classes that have similar functionality, along with storing these classes in a package.

Modules and Packages

In Python, modules are simply Python files with one or more classes and functions declared. For example, suppose we have a file called Customer. This would be a class that represents attributes and operations that can be performed with a Customer object, such as adding, removing, and modifying a Customer. With other languages like Java, you would normally have only a single class representing a single file, but it doesn't have be that way in Python. It's a matter of style and what the programmer prefers. A module with multiple class, function, and method names represents a namespace. For example, when accessing a class method from a module called Books, that method name is considered to be part of the Books namespace.

There are two ways to import modules in Python: by using the import module or the from module import syntax, where module is the name of the module. Let's take a look at using import, using two modules called Customer and Address. These two files contain the classes and methods necessary for associating one or more addresses with a customer. To import the Address module namespace into the Customer namespace, add the following line to the top of the Customer module:

import Address

Pretty simple. Next, to use the class objects from Address, all we need to do is use the dot (.) notation:

customerAddress = Address.CustomerAddress()
address = customerAddress.getAddress(custId)

What if we wanted to import just a single class or a set of specific classes from Address? Well, that's easy, too: Use the from module import syntax, followed by one or more class names, separated by commas:

from Address import CustomerAddress

To use the class objects from the class we imported, no dot notation is required:

customerAddress = CustomerAddress()
address = customerAddress.getAddress(custId)

We don't need to qualify the customerAddress class using dot notation with the module name because using this import syntax in Python loads the class attributes and methods directly into the namespace. In other words, the attributes and methods that were previously part of the Address namespace now belong to the Customer namespace. If you want to import all classes of a module using this syntax, simply do the following:

from Address import *

As programs grow larger, it becomes necessary to organize the modules into packages. To do that, we create a folder for each set of modules that will become a package. The package name is the name of the folder containing the modules. If you've programmed with Java, no doubt you'll be familiar with this concept.

When we create a package, Python needs to know that the folder is a package. In the folder, a blank file should be added called __init__.py. Python will look for this file; if it's not found, the folder won't be considered a package. The folders can be nested, providing different package levels. Packages are used by importing them into a module in one of two ways: as absolute imports or as relative imports.

For an absolute import, we specify the full path to the class we want to use, including the package name. For example, let's say we have a parent package called Employer. This package has the Company module. In another package called Employee (which belongs to Employer), this package has the Address and Customer modules. To call a method in the Company module from the Address module, use the full path to the address:

from Employee.Address import CustomerAddress
customerAddress = CustomerAddress()

or just use the import module syntax:

import Employee.Address
customerAddress = Address.CustomerAddress()

A relative import allows us to import modules without having to specify the package name. Instead, we use periods (dots) to indicate the number of levels where Python should look for a class or function. For example, to call the Address module from the Customer module, do the following:

from .Address import CustomerAddress

In the code above, the period before the Address module tells Python to look for this module in the same package. To call the Company module, which is inside the Employer parent package, go up a level from Customer by using another period:

from ..Company import CompanyAddress

Each period before the module represents bumping up one package level to look for the module.


In this article, you learned how to structure your Python programs by using modules and packages. With modules, it's important to note the two ways that modules can be imported syntactically, along with how to import them from packages using absolute or relative imports.

Packages are very much like they are with Java, using folders to represent package levels holding modules that are similar to each level.

At this stage, you should be comfortable creating your own small programs. Typically, it's a best practice to have a main.py file in the parent directory or top level of your package structure containing all the lifecycle logic to implement the application.

In Part 3 of this series, I'll cover inheritance in Python. This OO concept is important to developing Python programs that work with web frameworks such as Pyramid.

  • + Share This
  • 🔖 Save To Your Account