Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Python - Importing

There are three main ways to import modules in Python with subtle but important differences that we will cover below:

  • import [module name]
  • from [module name] import [class/variable/function name]
  • from [module name] import *

In our earlier tutorial where we created a basic Employee class. In this tutorial we are going to create an employee.py module which contains the class as shown below.

class Employee:
    'Common base class for all employees'
    __s_empCounter = 0 # static variable for incrementing IDs

    # Private member variables
    __id = None
    __name = None
    __salary = None

    def __init__(self, name, salary):
        self.__name = name
        self.__salary = salary
        Employee.__s_empCounter += 1
        self.__id = Employee.__s_empCounter

    def output(self):
        print("ID: ", self.__id)
        print("Name: " + self.__name)
        print("Salary: ", self.__salary)

We would then use the class in a separate script as shown below

jo = Employee("Jo Jarvis", 20000)
sue = Employee("Sue Smith", 21000)

jo.output()
print("-------------")
sue.output()

At this point, executing that script will fail. This is because we need to import the employee.py file with it's Employee class. To do this we can put the following line at the top:

import employee

This will look for the employee.py file and load everything within it. However, to then use the logic in that file we have to specify the whole path like so:

import employee

jo = employee.Employee("Jo Jarvis", 20000)
sue = employee.Employee("Sue Smith", 21000)

jo.output()
print("-------------")
sue.output()

This isn't very nice to write out and can easily lead us to exceeding any line length limits we have. Instead may be preferable to use:

from employee import Employee

jo = Employee("Jo Jarvis", 20000)
sue = Employee("Sue Smith", 21000)

jo.output()
print("-------------")
sue.output()

This looks much nicer because we can use Employee directly. However, in this scenario, any other classes/functions/variables that are defined in employee.py will not be loaded like they would be in the previous scenario. This could be resolved by adding additional imports such as:

from employee import Employee
from employee import Employer

jo = Employee("Jo Jarvis", 20000)
sue = Employee("Sue Smith", 21000)

jo.output()
print("-------------")
sue.output()

... or you can use a comma separated list like so:

from employee import Employee, Employer

jo = Employee("Jo Jarvis", 20000)
sue = Employee("Sue Smith", 21000)

jo.output()
print("-------------")
sue.output()

Finally, the last way to import modules and that is through the use of:

from employee import *

This is much like import core except that this imports all names except those beginning with an underscore _. In most cases Python programmers do not use this import facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.

For efficiency reasons, each module is only imported once per interpreter session.

The Import Search Path

When importing a module, Python searches in the following order:

  1. Check for a built-in module with that name (standard library)
  2. Search for a file with that name and the .py extension in a list of directories given by sys.path
    • Directory containing the executing script.
    • directories in the PYTHONPATH variable (like Java CLASSPATH
    • The installation-dependent default.

On file systems which support symlinks, the directory containing the input script is calculated after the symlink is followed. In other words the directory containing the symlink is not added to the module search path.

Last updated: 25th March 2021
First published: 16th August 2018