Python importlib – Importing Modules + Examples

In this tutorial we will explore the Python importlib module, and how we can use it to import other Python Modules programmatically in our code.

The importlib library has had a rather interesting history. It started out as a small library with only one purpose, which was to import a module using a string. Eventually however, this library expanded to include many other features regarding importing in Python, such as building custom finders, loaders, and importers, setup import hooks, and much more.


Importing Modules in Python with importlib

In this tutorial we’ll mostly be focusing on the basic and most common uses of importlib. The function for which it’s most known for, is import_module(). This is a function useful for when you don’t know the name of the module you will be importing while you are coding.

Normally you would simply do this:

import numpy

But if you don’t know the name of the library what will you do? With importlib, you can do the following:

import importlib

lib = importlib.import_module("numpy")

n = lib.sin(4)
print(n)

In the above example, we just imported the numpy library as an object called lib. We can now access all of the methods and modules from numpy using this object, such as sin.


Importing Sub-modules with importlib

There is another special case that can occur when importing modules. For example, many libraries have sub-modules within them. An example of this is the Python Matplotlib Graphing Library. This library has a very popular sub-module called pyplot, which is used to draw graphs.

The below example shows how we can import the pyplot module from matplotlib, and use one of its functions.

import importlib

lib = importlib.import_module("matplotlib.pyplot")

lib.plot([1, 2, 3, 4], [1, 4, 9, 16])
lib.show()

Basically, you just need to add a “.” after the name of the library, then mention the submodule’s name.


Importing Python Files as Modules

Another possible case, is where instead of importing libraries from the Standard Python Library, you have your own custom libraries you wish to import. These may be located somewhere on your file system.

For example, the Module we wish to import might be located in the same directory as the Python file importing it. In this case, we can just import it as we have been doing before.

But in the event that the Module we wish to import is in a sub-directory, then we need to do two things.

First we need to make a file inside the sub-directory, called __init.__py. The director structure should look something like this:

test.py
subdir/
    __init__.py

    myfile.py

We can now go ahead and import “myfile.py” using the following code.

import importlib

lib = importlib.import_module("subdir.myfile")

The format is pretty simple. To traverse sub-directories, we use a . in the same way we traverse through sub-modules. Don’t include the .py in the extension, otherwise it will think you are trying to import a file from another sub-directory called py.


This marks the end of the “Importing Python Modules with importlib” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments