Modules in python featured image

Importing Modules in Python 3: A Comprehensive Guide

Introduction

Most of us are familiar with Python as a programming language. One of the great things about Python 3 is the myriad of built-in functions it offers. You get access to these functions when you are using Python 3 to write different types of code. Before we start, take a look at our tutorial on installing Python 3 and setting up a local programming environment on Ubuntu 16.04. Now, let’s take a look at some of Python’s helpful functions:

  • print(): This function enables you to print out expressions.
  • abs(): With this function, you can obtain the absolute value of any number.
  • int(): This one lets you convert any given data type into an integer.
  • len(): It shows you the length of any sequence or collection.

Are Built-In Functions Sufficient?

Unfortunately, these built-in functions are not enough on their own. When programming in Python 3, you may find yourself requiring more functionality. You do not want to be limited by only the default features of the program. That is where modules come into the picture.

What are Modules?

Modules allow you to build more sophisticated programs. They open up your options and give you more room to work with. Modules typically comprise Python .py files. Each of these files contains bits of Python code. You can reference any Python file as a module. Using these modules, you can do all sorts of things. For example, you could define functions, classes, as well as variables. You can reference these in other Python .py files or through the command line interpreter.

Let’s say you have a Python file called hello.py. The module name of this file is hello. You can import this module to other Python files easily. You can also use it on the Python command-line interpreter. The way you import modules on various files is by using the import statement. When you import a module, you are basically executing the code inside the module. You are making the definitions in the module usable for the current file.

In this comprehensive guide, we will cover how you can check for and install modules in Python 3. We will also talk about how to import your modules and alias them.

How to Check for Modules?

As we mentioned before, there are some built-in modules in Python 3. These can be found in the Python Standard Library. This library contains several standard modules that perform some basic system functions and solutions. It is a default feature that you receive when you install Python.

If you need to check whether these modules are ready for use, you need to give a command. You will need to enter either the local Python 3 programming environment or the server-based programming environment. Then you will have to start the Python interpreter in your command line. It will look something like this:

Now, you can run the import statement. This will confirm that the module can be called in when you need to. Let’s say you run the command with the math module:

The math module is a built-in module in Python. This means that the interpreter will complete the task without any feedback. It will return you to prompt. You can start using the module right away without any other action.

How to Install Modules?

On the other hand, let’s say you want to run a module that is not installed. In our example, we will consider the 2D plotting library matplotlib. Here is how you would run the import statement with such a module:

If you did not install the module, you will get an error like this:

To make it work, you need to first deactivate the Python interpreter. You can do that with CTRL + D. Next, you must install matplotlib with pip:

Now you will be able to successfully import matplotlib in the interpreter using the import matplotlib command. This time, it will not return an error.

How to Import Modules?

Let’s explore how to import modules. To utilize its functions, you have to import the module using the import statement. A statement comprises the import keyword paired with the module name. The module name is typically present at the top of the code in a Python file.

For example, if you are looking at a Python file called my_rand_int.py, you will import the random module to generate random numbers:

You will have to refer to the function in dot notation. It would look something like this: [module].[function]. It basically means that you can use the module as a separate namespace in the current file.

Continuing with the module, let’s look at some functions:

  • random.randint(): This function will return a random integer.
  • random.randrange(): This function will return a random element from a given range. Let’s see how you can make a for loop. We will be calling one of the functions for the random module in our current program which is my_rand_int.py:
The program will import the module and then go into a for loop. As you can see, the loop will work with 10 elements. The program will print out a random integer within the inclusive range of 1 to 25. This means that the integers of 1 and 25 are the parameters of random.randint().

Now when you run the program with python my_rand_int.py, you will get 10 random integers in the output like this:

Since we used the random module, you will get new integers every time. They will not, however, go above or below 25. Sometimes, you can also use multiple import statements simultaneously like so:

We will use our extra module by adding pi from math to the program. We will decrease the number of random integers as well:

Now our output will look like this:

We have five random integers and the approximate value of pi.

How to Use from….import?

Apart from import, another statement you should know about is from … import. This is useful for when you do not want to refer to your functions using dot notation. This way, you can call the functions by name and specify references directly. In this example, we will be importing the randint() function from the random module:

We directly specify the function we want to call. Even in our program, we only have to mention the name of the function without the dot notation:

The output will be the same as the one you saw above. The difference is that you can now avoid using dot notation altogether.

How to Alias Modules?

You should also be familiar with the as keyword. Using this keyword, you can change the module names and their functions. You may need to do this if you have an existing or newly imported module that is using the same name. The statement will appear as such for aliasing modules:

In our example, we will modify the name of the math module. We will abbreviate it and change it to m. The program will look like this:

As you can see, instead of referring to pi as math.pi, we are calling it m.pi.

Certain modules have official aliases. This is because it is very common for individuals to use aliases instead of their actual names. Take the matplotlib.pyplot module into consideration. In the official documentation, its alias is plt. You would make the alias as such:

Conclusion

The ability to add and import modules really expands the world of Python programming. It allows you to reach beyond just the built-in functions. You can simultaneously use the default Python modules as well as the ones you install using pip. Make sure that you utilize this feature when creating something more complex or sophisticated. It makes your work easier, allows for creativity, and helps build strong programs.

Finally, here are some more resources from our blog that will help you further utilize Python:

Happy Computing!