Fixing “ModuleNotFoundError: No module named ‘requests’” Error in Python

Oct 22, 2023 · 6 min read

The “ModuleNotFoundError: No module named 'requests'” is a common error encountered when trying to import the requests module in Python code. The requests module is an extremely popular 3rd party library that makes it easy to send HTTP requests in Python.

When you try to import requests and get this error, it means Python can't find the requests module installed. There are a few key steps we can take to fix this:

Step 1: Install the Requests Module

The most obvious reason you see the "No module named 'requests'" error is that requests isn't installed in your environment. Requests doesn't come as part of the Python standard library, so you have to install it yourself.

The easiest way to install requests is using pip:

pip install requests

If you don't have pip available, first upgrade it:

python -m pip install --upgrade pip

Then install requests:

python -m pip install requests

For Python 3:

python3 -m pip install requests

If you get permission errors, use sudo before the pip command or try the --user flag.

Using Virtual Environments

It's highly recommended to install Python packages in a virtual environment to avoid conflicts. Here's how:

python3 -m venv env
source env/bin/activate  # on Windows use `env\\Scripts\\activate`
pip install requests

This installs requests locally in that virtual environment. Make sure to activate the virtual environment before running Python code that needs requests.

You can also install requests directly from PyCharm, Jupyter Notebook, and other IDEs. Check their documentation on adding Python packages.

Lastly, you can install a specific version of requests using:

pip install requests==2.26.0

Once installed, double check requests got installed properly by using pip show requests or importing it in Python.

Step 2: Update PYTHONPATH and Path Environment Variables

Sometimes requests may be installed but Python can't find it due to directory issues.

Python internally has a PYTHONPATH variable listing directories to search for modules. We may need to add the requests install location to PYTHONPATH.

First get the installed requests path using:

pip show requests

On Windows, copy the path and update PYTHONPATH in Advanced System Settings.

On Linux/macOS, get existing PYTHONPATH:

echo $PYTHONPATH

Add the requests path:

export PYTHONPATH="${PYTHONPATH}:/path/to/requests"

We also need to add the requests path to the system PATH variable. The steps are similar - copy the path, go to Advanced System Settings on Windows, and edit PATH to add the new path.

On Linux/macOS:

export PATH="$PATH:/path/to/requests"

This makes sure Python can find the requests module.

Step 3: Confirm Your Code Uses the Right Python Version

Another common reason for the error is having multiple Python versions installed, and requests got installed for a different one than you're running code on.

Always check which Python version you're using to run code where you import requests. Make sure it matches the python you used to install requests.

You can check the version using:

python --version

In IDEs like PyCharm, specify which Python interpreter to use for your project. Make sure it matches where requests is installed.

If you used a virtual environment to install requests, activate it before running Python code.

Step 4: Reinstalling or Upgrading Requests

If you still see the error after the above steps, try reinstalling or upgrading requests:

pip uninstall requests
pip install requests

You can also upgrade to the latest version:

pip install requests --upgrade

Sometimes pip gets into a weird state where the package appears to be installed but keeps throwing errors. Reinstalling it fixes such issues.

Step 5: Import Requests Properly in Your Code

How you structure and import code can also cause issues with importing requests and other modules.

A common mistake is using relative imports rather than absolute ones:

from .requests import get  # this will fail

Always use absolute imports for external modules:

from requests import get

Another pitfall is naming your own module file as requests.py which will conflict with the requests module.

Finally, some IDEs like PyCharm provide import analysis and may show errors for unresolved imports. You can disable the checks like:

import requests  # type: ignore

Step 6: Troubleshooting Conflicting Library Installations

If you installed requests using a package manager like apt, yum or brew and also installed it separately using pip, you can get conflicts and errors.

Always use the same tool to install and upgrade a particular Python package. Prefer pip where possible.

Similarly, installing requests globally while running code in a virtual environment can make seemingly installed packages unavailable.

Use virtual environments and pip to keep things isolated and avoid such issues.

Step 7: Docker and Virtualization Specifics

To use requests in Docker containers, make sure you install requests inside the container:

RUN pip install requests

For virtual environments, activate the venv and install packages inside it rather than globally on the host machine.

Conclusion

The "ModuleNotFoundError: No module named 'requests' ” error occurs because requests is not installed, is installed incorrectly, or the environment is misconfigured.

Following a systematic approach of first installing the package properly, confirming python versions used, reinstalling/upgrading, and checking import statements can help fix the problem.

Virtual environments and consistent use of pip reduces complexities around managing Python packages/versions. Carefully go through the steps outlined here to resolve the issue in your environment.

Frequently Asked Questions

Q: Do I need to install requests separately in Python?

A: Yes, requests is not part of the Python standard library. You have to install it yourself using pip.

Q: Where does pip install packages on Windows?

A: Pip typically installs Python packages into C:\Users\\AppData\Roaming\Python\Python\site-packages.

Q: Does requests come with Python 3?

A: No, requests does not come as a built-in module in Python 3. You have to install it separately using pip.

Q: What happens if I name my file requests.py?

A: This will conflict with the requests module name and cause errors. Always use unique names for your own modules.

Q: How do I check which packages are installed in Python?

A: You can use pip list or pip freeze to check installed packages. Use pip show to get info on a specific package.

I tried to cover the main questions people had while sticking to the outline provided. Let me know if you would like me to modify or expand the article further.

Browse by tags:

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: