Modules in Python

Creating modules #

Different languages provides different ways of modularizing code. In Python, we use modules to organize large programs. Python standard library uses modules extensively to organize related functions, classes, variables and constants. We have already used some built-in modules like math, datetime, random; etc a few times in earlier lessons. You don't have need to organize your programs into modules, however if your program is several pages long or you want to reuse your code, you should definitely use modules.

So What is module anyway ?

A module simply a Python file that ends with .py extension. We can define a group of classes, functions, variables, constants and so on inside a module. To reuse the classes or functions defined inside the module, we have to import the module in our program using the import statement. Its syntax is:

import module_name

where module_name is the name of the file without .py extension

The import statement searches the module, parses and execute it's content and makes it available to the client program. A client program or simply client is a program which uses a classes, functions and variables defined in a module without knowing the implementation details. To refer to the classes, function or variables in the client program prefix it with module name. For example, to call a function named timer() defined inside a module named great_module, in a client program do this:

great_module.timer()

If import statement failed to find the module, ImportError error will be generated.

Let's take an example:

Create a new file named my_module.py and add the following code to it.

python101/Chapter-14/my_module.py

A_CONSTANT = 100

name = 'a variable'

def great_printer():
    print("first great_printer() definition")

def doo_hickey(arg):
    print("doo_hickey() called with argument:", arg)

Now we create a separate program(or client) named test_module.py in the same directory as my_module.py with the following code.

python101/Chapter-14/test_module.py

import my_module

my_module.doo_hickey('www')
my_module.great_printer()
print(my_module.A_CONSTANT)
print(my_module.name)

Output:

doo_hickey() called with argument: www
first great_printer() definition
100
a variable

In a module, identifiers used to name entities like class, function and variables must be unique. If two entities using same name is found then the Python will use the last definition. For example, open my_module.py file and add another function named great_printer() at the end of the file as follows:

A_CONSTANT = 100

name = 'a variable'

def great_printer():
    print("first great_printer() definition")

def doo_hickey(arg):
    print("doo_hickey() called with argument:", arg)

def great_printer():
    print("second great_printer() definition")

Now we have two functions using the same name. As a result, Python will use the last definition of great_printer() function. Run the client program test_module.py and you will get the following output:

Output:

doo_hickey() called with argument: www
second great_printer() definition
100
a variable

Importing Selected Identifiers #

The statement import my_module imports every identifier in the module to the client program. In some cases, we only want to use some specific names from the module. Let's say from the module my_module.py, we only want to import doo_hickey() function and name variable in our client program. For situations like this, there exists another form of import statement which allows us to import only specific names from the module. Its syntax is:

from module_name import name1, name2, ... nameN

where name1, name2 and so on are names of the entities we want to import in our client program. Any code after this import statement can use name1, name2 and so on without prefixing it with module name.

Here is a program which imports only doo_hickey and name identifier from the module my_module.

from my_module import doo_hickey, name

doo_hickey('x')
print(name)

Output:

doo_hickey() called with argument: x
a variable

In case you want to import every identifier into your client program do this:

from module_name import *

Here * indicates all identifiers. This statement is functionally equivalent to import module_name. The only difference is that the former one allows to access identifiers in a client program without prefixing it with module name.

Note: import module_name will not import names that start with double underscore character.