pytest: ModuleNotFoundError: No module named ‘requests’

Dirk Avery
Jan 18, 2019 · 3 min read

pytest is an outstanding tool for testing Python applications. However, when using pytest, there’s an easy way to cause a swirling vortex of apocalyptic destruction called “ModuleNotFoundError.” Fortunately, fixing the issue is really easy.

This is the error you might have seen:

______ ERROR collecting tests/test_satsuki.py ____________________
tests/test_satsuki.py
:10: in <module>
import requests
E ModuleNotFoundError: No module named 'requests'
______ ERROR collecting tests/test_satsuki.py ____________________
ImportError while importing test module '/Users/username/dev/satsuki/tests/test_satsuki.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_satsuki.py:10: in <module>
import requests
E ModuleNotFoundError: No module named 'requests'

Instead of Requests, maybe you’re missing Scrapy or Pyglet or NumPy or Pandas. The point is, pytest says the module is not found but you are sure it exists. You can even prove it is there:

$ pip install requests
Requirement already satisfied: requests in /Users/username/.local/share/virtualenvs/satsuki-7hJc15h4/lib/python3.7/site-packages (2.21.0)

Why is pytest cursing me with the ModuleNotFoundError?

This issue is not the module or package you’re trying to use. We’ve shown that it is there. Installing, uninstalling, and reinstalling won’t fix this. Reinstalling your virtual environment won’t fix it. Resynching Pipenv won’t fix it.

The issue is which pytest you are using and your use of a virtual environment.

If you have installed pytest system-wide, in other words, outside of a virtual environment, pytest has a nasty habit of only looking outside your virtual environment for modules! If your project is using a module that is only installed in your virtual environment, and you’re using a system-wide pytest, it won’t find the module, even if you’ve activated the virtual environment.

Let’s put this another way. Here’s how you could recreate the problem:

  1. Install pytest outside of all virtual environments. This way it is system wide. For example:
    $ pip3 install pytest
  2. Now, make sure that the package you need is not installed system wide. For example:
    $ pip3 uninstall requests
  3. Activate a virtual environment for your project that needs the package. For example:
    $ pipenv shell
  4. Install the package in the virtual environment. For example:
    $ pip install requests
  5. You should now be able to see both pytest and the package. If you check pytest and the package, it will give you your first hint that there’s a problem — notice where pytest is located versus where the package is located:
    $ pytest --version
    This is pytest version 4.1.0, imported from /usr/local/lib/python3.7/site-packages/pytest.py
    $ pip install requests
    Requirement already satisfied: requests in /Users/username/.local/share/virtualenvs/satsuki-7xyz/lib/python3.7/site-packages (2.21.0)
  6. Run pytest and get your ModuleNotFoundError!

Fixing pytest and the ModuleNotFoundError

Simply uninstall pytest from your system and only install it within a virtualenv when you need it.

Here’s the step-by-step:

  1. Exit any virtual environment
  2. Use Pip to uninstall pytest
  3. Activate your project’s virtual environment
  4. Install pytest inside the virtual environment
  5. pytest will now find your virtual-environment-only packages!

An example of fixing it on the command line:

(venv) $ exit
$ pip3 uninstall pytest
Uninstalling pytest-4.1.0:
Would remove:
/usr/local/bin/py.test
/usr/local/bin/pytest
/usr/local/lib/python3.7/site-packages/_pytest/*
/usr/local/lib/python3.7/site-packages/pytest-4.1.0.dist-info/*
/usr/local/lib/python3.7/site-packages/pytest.py
Proceed (y/n)? y
Successfully uninstalled pytest-4.1.0
$ pipenv shell
(venv) bash-5.0$ pip install pytest
Collecting pytest
Downloading https://files.pythonhosted.org/packages/9e/bf/2974be45c498a0ebc2708bfada25d5d1874ab3315a4e76ce7d38e29724fa/pytest-4.1.1-py2.py3-none-any.whl (216kB)
100% |████████████████████████████████| 225kB 7.8MB/s
Successfully installed pytest-4.1.1
$ pytest
==================== test session starts ===================
platform darwin -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: /Users/username/dev/satsuki, inifile: setup.cfg
collected 8 items
tests/test_satsuki.py ........ [100%]================ 8 passed in 19.75 seconds =================

Yay!

Dirk Avery

Written by

Cloud developer, AI buff, patent attorney, fan of cronuts. AWS Certified Solutions Architect — Professional. Go, Python, Bash. https://www.plus3it.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade