How To Fix ModuleNotFoundError: No Module Named ‘Pytest’ In Python?

ModuleNotFoundError: No module named 'pytest' in Python

If you are using Pytest to test Python code and accidentally get the “ModuleNotFoundError: No module named ‘pytest'” in Python, let’s read this article carefully. It will help you fix the above error in the simplest way.

What is Pytest?

Unlike most programming languages, Python uses a built-in library for testing called pytest. Pytest is a framework that makes building simple and scalable tests easy.

When does the ModuleNotFoundError: No module named ‘pytest’ in Python occur? And how to fix it?

Import pytest, but not installed

“ModuleNotFoundError: No module named ‘pytest'” in Python appears when we import pytest and run the test while we don’t have the pytest module installed. See the code below for how the error occurs:

# Import pytest but not installed
import pytest

def sum(a, b):
    return a + b

def test_function():
    assert sum(3, 5) == 8

Output:

ModuleNotFoundError: No module named 'pytest'

To fix it, install pytest by opening a terminal and typing the following command:

pip install pytest

Now all we need to do is importing and using pytest:

import pytest

def sum(a, b):
    return a + b

def test_function():
    assert sum(3, 5) == 8

Output:

Import with the wrong name

Some libraries must capitalize the first letter in Python when importing, but pytest is different. We do not need to capitalize the first letter of the pytest.

# Import wrong name
import Pytest

def sum(a, b):
    return a + b

def test_function():
    assert sum(3, 5) == 8

Output:

ModuleNotFoundError: No module named 'Pytest'

Because pytest and Pytest are different modules, so you can fix this error by using import pytest instead of import Pytest.

import pytest

def sum(a, b):
    return a + b

def test_function():
    assert sum(3, 5) == 8

Output:

Install pytest manually but forgot to run setup.py

This error also appears when you install pytest using the package downloaded here. And forgot to run the command:

python setup.py install

Below is the complete guide to help you install pytest manually:

After downloading the installation package, open the terminal and type:

where python

The returned result will be the path to the Python installation directory. As below:

You go to the Python310 directory as the above path. Then continue to go to the folder: \Lib\site-packages\.

Here, you paste the pytest installation file and extract it. After extracting, you will get the folder like this:

You’re mistaken if you think we have successfully installed pytest in this step. Here is an example if you use pytest in this step.

# Haven't run setup.py
import pytest

def sum(a, b):
    return a + b

def test_function():
    assert sum(3, 5) == 8

Output:

ModuleNotFoundError: No module named 'pytest'

As you can see, the error ModuleNotFoundError: No module named ‘pytest’ still appears.

To complete the installation, move to the pytest folder and open a terminal, and type:

python setup.py install

If you see the following message:

Finished processing dependencies for pytest==7.1.3

That means you have successfully installed it. Now you can use pytest without the “ModuleNotFoundError: No module named ‘pytest'”.

import pytest

def sum(a, b):
    return a + b

def test_function():
    assert sum(3, 5) == 8

Output:

Summary

That’s for the “ModuleNotFoundError: No module named ‘pytest'” in Python. If you have any questions, please leave us a comment. We will respond as possible. Thank you for reading!

Have a nice day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *