Modules in Python

Creating modules

Different languages provide 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 the earlier lessons. If you have written a small program you don’t really 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 a module?

A module is an ordinary 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. The syntax of import statement is as follows:

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 classes, functions or variables defined in a module without knowing the implementation details. To refer to the module class, function or variables in the client program prefix it with the module’s name. For example, to call a function named timer() defined inside a module named great_module, in a client program do this:

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

Let’s take an example:

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


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



In a module, identifiers used to name entities like class, function or variables must be unique. If two entities using the same name is found then the Python will use the last definition. Create a new file named and add the following code to it:


The code is exactly the same as, the only difference is that here we are defining another function named great_printer() at the end of the file.

Now we have two functions using the same name (great_printer). As a result, Python will use the last definition of great_printer() function. Create another client program named with the following code:



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, 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:

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 the module name.

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



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

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 the module name.

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

Leave a Comment

%d bloggers like this: