How To Import Modules in Python 3
Introduction
The Python programming language comes with a variety of built-in functions. Among these are several common functions, including:
Prerequisites
You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)
Checking For and Installing Modules
There are a number of modules that are built into the Python Standard Library, which contains many modules that provide access to system functionality or provide standardized solutions. The Python Standard Library is part of every Python installation.
To check that these Python modules are ready to go, enter into your local Python 3 programming environment or server-based programming environment and start the Python interpreter in your command line like so:
From within the interpreter you can run the import statement to make sure that the given module is ready to be called, as in:
Let’s run the import statement with a module that you may not have installed, like the 2D plotting library matplotlib:
If matplotlib is not installed, you’ll receive an error like this:
Output
ImportError: No module named 'matplotlib'You can deactivate the Python interpreter with CTRL + D and then install matplotlib with pip. Next, we can use pip to install the matplotlib module:
Once it is installed, you can import matplotlib in the Python interpreter using import matplotlib, and it will complete without error.
Importing Modules
To make use of the functions in a module, you’ll need to import the module with an import statement.
An import statement is made up of the import keyword along with the name of the module.
In a Python file, this will be declared at the top of the code, under any shebang lines or general comments
So, in the Python program file my_rand_int.py we would import the random module to generate random numbers in this manner:
In practice, with the example of the random module, this may look like a function such as:
Let’s create a for loop to show how we will call a function of the random module within our my_rand_int.py program:
for i in range(10):
print(random.randint(1, 25))
When we run the program with python my_rand_int.py, we’ll receive 10 random integers as output. Because these are random you’ll likely get different integers each time you run the program, but they’ll generate something like this:
Output
6,9,1,14,3,22,10,1,15,9If you would like to use functions from more than one module, you can do so by adding multiple import statements
import random
import mathTo make use of our additional module, we can add the constant pi from math to our program, and decrease the number of random integers printed out:
import random
import math for i in range(5): print(random.randint(1, 25)) print(math.pi)Now, when we run our program, we’ll receive output that looks like this, with an approximation of pi as our last line of output:
Output
10,7,13,10,3.141592653589793The import statement allows you to import one or more modules into your Python program, letting you make use of the definitions constructed in those modules.
Using from … import
To refer to items from a module within your program’s namespace, you can use the from … import statement. When you import modules this way, you can refer to the functions by name rather than through dot notation
In other programs, you may see the import statement take in references to everything defined within the module by using an asterisk (*) as a wildcard, but this is discouraged by PEP 8
Let’s first review at importing one specific function, randint() from the random module:
from random import randint
Now, when we implement this function within our program, we will no longer write the function in dot notation as random.randint() but instead will only write randint():
from random import randint
for i in range(10):
print(randint(1, 25))Using the from … import construction allows us to reference the defined elements of a module within our program’s namespace, letting us avoid dot notation.
Aliasing Modules
You may want to change a name because you have already used the same name for something else in your program, another module you have imported also uses that name, or you may want to abbreviate a longer name that you are using a lot.
The construction of this statement looks like the following:
import [module] as [another_name]
Let’s modify the name of the math module in our my_math.py program file. We’ll change the module name of math to m in order to abbreviate it. Our modified program will look like this
import math as m
print(m.pi)
print(m.e)Within the program, we now refer to the pi constant as m.pi rather than math.pi
import matplotlib.pyplot as plt
This allows programmers to append the shorter word plt to any of the functions available within the module, as in plt.show(). You can see this alias import statement in use within our “How to Plot Data in Python 3 Using matplotlib tutorial.”
Conclusion
When we import modules we’re able to call functions that are not built into Python. Some modules are installed as part of Python, and some we will install through pip
Making use of modules allows us to make our programs more robust and powerful as we’re leveraging existing code. We can also create our own modules for ourselves and for other programmers to use in future programs