Python is accompanied with a number of built-in modules that allow you to perform common operations in your
code. int(), for example, converts a value to an
integer. sum() calculates the sum of all items in a list.
While these functions can be useful, you may want to define your own that can be used throughout your codebase. That’s where Python modules come in.
In this tutorial, we’re going to discuss the basics of Python modules, and how to import a module into your code using the Python import statement.
.py files that contain Python code.
The benefit of using modules in your code is that they allow you to reference pre-written code that can automate common tasks in your program.
For instance, if you are building a complex program that needs to read a lot of data from a file, you could write a module to perform that action. This means that you can store the file reading code elsewhere, which will help you make your main program easier to read.
To access a module in Python, you can use the import statement. The import statement reads the code in a Python module and allows you to use it in another file.
Many of the modules you can use in your programs are part of the Python Standard Library. This library is part of every Python instance, which means you can import a module contained in the Python Standard Library without installing a module into your system. You can also use pip to install modules, or even create your own.
Import a Python Module
Once you’ve installed a module on your system—or have built your own—you will need to import it into your code.
import statement allows you to import a module into your code.
import statements appear at the top of every Python file, beneath any comments that may exist. This is because importing modules at the top of a file is quicker than doing so later in your code.
Suppose we want to import the module called
time into a program. The
time module is part of the Python Standard Library, so we do not need to install it into our code. We could import the module using the following code:
Now that we have imported this module, we can access it in our code. In the above example, we have imported
time into our code, so now we can access all the
time functions in our main program.
To use the code in a module, we need to reference the unique namespace for the module. We can do so by using the dot notation.
So, suppose we want to use the
sleep() function in the time module. We could do so using this code:
In this code, we first import the time module. Then, we use “
time.sleep” to reference the sleep() function in the time library. Dot notation, then, is where the module name is called, followed by a dot, then the name of the function we want to access.
Let’s walk through another example to illustrate how this works in more depth.
The following program uses the time module to retrieve the current time:
import time timestamp = time.time() current_time = time.ctime(timestamp) print(current_time)
Our code returns:
Fri Apr 17 08:04:28 2020
Let’s break down our code line-by-line.
- import time allows us to import the time module into our code.
- time.time() gets the current time in seconds, using the time module.
- time.ctime() converts the time in seconds to a readable time.
- print(current_time) prints the readable time created by time.ctime().
In this code, we reference two functions from the time module: time.time() and time.ctime(). If you’re interested in learning more about how the Python time module works, read our beginner’s guide to Python time.
Import Multiple Modules
So far, we have imported one module into our code. However, we can import as many different modules as we want.
Suppose we want to import the
random modules (which are both part of the Python Standard Library) into our code. We could do so using this code:
import time import random
Now that we have imported these modules, we can use their functions in our code. Suppose we want to generate a random number between 1 and 10, then get the current time. We could do so using this program:
import time import random print(random.randint(1, 10)) timestamp = time.time() current_time = time.ctime(timestamp) print(current_time)
Our code returns:
Fri Apr 17 08:09:22 2020
In our code, we first import the “time” and “random” modules. Then, we use the random.randint() function to generate a random number. Next, we use the same code from our earlier example to retrieve the current time and print it to the console.
Using the from Statement
The import statement allows you to import all the functions from a module into your code.
Often, though, you’ll only want to import a few functions, or just one. If this is the case, you can use the from statement to import only the exact functions you are going to be using in your code.
The advantage of using the from statement is that it allows you to be more specific about what you want to import into your code.
The syntax for using the from statement is:
from [module] import [function]
Suppose we only want to import the choice() function from the “random” library into our code. We could do so like this:
from random import choice fruits = ["Apple", "Pear", "Banana"] print(choice(fruits))
Our code returns:
In our code, we use the
from keyword, followed by
random, to tell our program that we want to import a specific function from the “random” module. Then, we use the
import keyword to tell our code what function we want to import.
When using the
from keyword to import a function, you do not need to write the function using dot notation. As you can see above, instead of using
random.choice() to access the
random.choice() function from the random module, we just use
The default name given to modules in your code is the name of the module.
However, you can override the default module name using the as keyword. This is useful if the name of a module conflicts with a variable you have declared in your code, or another module that shares the same name.
Suppose we want to import the time module into your code. Instead of importing the time module and using its default name, we are going to change it to
t. We could do so using this code:
import time as t print(t.time())
Our code returns:
In our code, the
time library has been given the alias
t. This means that, when we want to reference any function in the time library, we must use
t and the dot notation.
Importing modules is an important part of working with Python that allows you to call functions that are not part of your main program. The modules you work with can be part of Python, installed using pip, or created by you.
This tutorial discussed, with examples, the basics of Python modules and how to import them in your code. Now you’re ready to start importing modules like a professional Python coder!
Python is an in-demand skill in the technology industry. Download the free Career Karma app today to discover, with help from a career coach, how you can kickstart your journey to a career using Python in only 21 days.