The ModuleNotFoundError: No module named ‘pytest’ in Python

We were trying to utilize the pytest module in the virtual environment but encountered ModuleNotFoundError: No module named pytest. Let’s find all of the answers to this problem here in our article. Without further discussion, let’s begin solving the error.

How Does The Error ModuleNotFoundError: No module named ‘pytest’ Occur?

As mentioned earlier, the error occurred when we tried to utilize the pytest package in the virtual environment. The error is as follows.

ModuleNotFoundError: No module named ‘pytest’”

How To Solve The Error?

If you’re executing Virtual Environment, follow this procedure to resolve the problem. Set up a project folder. Navigate to the project folder. Execute python3 -m venv <name_of_virtualenv>. After that, launch your virtual environment.

For Windows, use \path\to\env\Scripts\activate. While with Posix systems, use the command source /path/to/ENV/bin/activate. Then add pytest into your virtualenv. It should resolve your problem by now.

Solution 1

The procedure to fix the problem is demonstrated as below.

  • Establish a project folder
  • Go to the project folder
  • Execute python3 -m venv <name_of_virtualenv>
  • Activate virtualenv
  • With Windows, use: \path\to\env\Scripts\activate
  • With Posix systems: /path/to/ENV/bin/activate
  • Install pytest in the virtualenv
And you may use pytest normally again as your problem has been fixed.

Solution 2

Even after installing the pytest module, the problem may continue. Most likely, you install the pip, but not in a location you can use. Although you may have installed the pip on the platform, the script still cannot identify it. As a result, it cannot install the module in the right directory.

Follow the instructions below to resolve the path issue in Windows.

Step1: Start the command and type python to navigate to the place where you installed it.

Step2: After you’ve opened the Python file, go to the Scripts file and copy the address. Also, ensure that the pip package is present in the directory.

Step 3: In the command window, use the previously copied address and the cd command to access the Scripts folder

Step 4: Execute pip install pytest order to install the module.


Our article has helped you figure out how to fix the ModuleNotFoundError: No module named ‘pytest’. We know that some of these solutions may not be ideal, but they are much better than the error itself. 

If you find any questions or concerns about this error or need more help, please type them in the comment section anytime. We are always happy to help.

Related articles

Scroll to Top