Modules are integral to the Python language. Modules let you break down your code into multiple files. This helps maintain the readability and maintainability of a codebase.
It’s common to encounter a ModuleNotFoundError when working with modules. In this guide, we talk about what this error means and why it is raised. We walk through three possible solutions to help you overcome this error.
What is a ModuleNotFoundError?
A ModuleNotFoundError is raised when Python cannot successfully import a module.
The full error message looks something like this:
ModuleNotFoundError: No module named 'your_module_name'
This error is encountered when you forget to install a dependency for a project. Because you haven’t installed the dependency, Python does not know where to locate it.
ModuleNotFoundErrors come up in user-defined modules. Often, this error is caused by importing files relatively when doing so is not allowed.
Example Scenario: Dependency Not Installed
Take a look at a file called app.py that uses the BeautifulSoup package:
from bs4 import BeautifulSoup url = "https://careerkarma.com" print(url)
Let’s try to run this file:
Traceback (most recent call last): File "app.py", line 1, in <module> from bs4 import BeautifulSoup ModuleNotFoundError: No module named 'bs4'
It looks like Python 3 cannot find the module “bs4”. Because bs4 is an external package, the cause of this error should be that we haven’t installed the module.
To solve this error, we install the bs4 module:
pip3 install BeautifulSoup4
This code installs the module required for our project.
Note: You should ensure you use the right package manager to install a module. You cannot install modules for Python 3 using pip and modules for Python 2 using pip3. If you do, the Python interpreter may not recognize that you have installed a module.
Run our code again:
Our code works!
Example Scenario: User-Defined Modules
There are two types of imports in Python: absolute and relative.
Absolute imports are where you import something on sys.path, like a built-in package. Relative imports are where you import something that is relative to the program that you are writing.
Relative imports must be part of a package otherwise they cannot be run.
Next, we write a program that prints out a list of cakes to the console. First, we create a directory structure for our project:
app.py config.py cakes/ main.py
We start by declaring a list of cakes in our “config.py” file:
cake_list = ["Double Chocolate Gateau", "Victoria Sponge", "Lemon Cake"]
Next, we write our “main.py” file which prints these cakes to the console:
def print_cakes(cakes): for c in cakes: print(c)
Finally, we write our main program in app.py which executes our module:
import main import config main.print_cakes(config.cake_list)
This code runs the
print_cakes() function inside the “main.py” file. We import “main” and “config” so that we can access our
print_cakes() function and our list of cakes. This list of cakes is passed through our
Run our code and see what happens:
Traceback (most recent call last): File "app.py", line 2, in <module> import config ModuleNotFoundError: No module named 'config'
This error occurs because we have not imported our files successfully.
“main” is in the “cakes” module. It is not in our current working directory. We know this because “main.py” is in the “cakes” folder. To access this module, we need to import it relatively:
from cakes import main import config main.print_cakes(config.cake_list)
Instead of importing “main” directly, we import “main” from the “cakes” module. Let’s see what happens when we run our code again:
Double Chocolate Gateau Victoria Sponge Lemon Cake
Our code prints out the list of cakes. We have imported “main” from “cakes” and resolved our ModuleNotFoundError error.
Notice that we import the “config” module directly into our code. This is because “config” is in the same folder as the program that we are executing.
The ModuleNotFoundError is raised when Python cannot locate an error. The most common cause of this error is forgetting to install a module or importing a module incorrectly.
If you are working with an external module, you must check to make sure you have installed it. If you are writing a user-defined module, you must double-check your import statements and ensure that they all import files relatively.
Now you’re ready to solve the ModuleNotFoundError Python error like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.