ModuleNotFoundError: No module named ‘requests’ in Python

The Python ModuleNotFoundError: No module named ‘requests’ in Python error occurs when we didn’t install the library requests before importing it. To solve this error, run pip install requests in your terminal and execute your code again.

#Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'requests'
Code language: Python (python)

Open your terminal, navigate to your project’s root directory and install the requests library. The exact instructions you have to execute depend on the OS you are using, though.

Open the terminal application and use pip install requests or pip3 install requests (for Python 3). This installation will only work if you have pip.exe installed and pip.exe added to the PATH environment variable. If you don’t, run python -m pip install requests or python3 -m pip install requests.

pip is the package installer for Python and comes by default when you install it

You can

  • also install the requests library manually by downloading it from the official website and add the files (unzipped) to /Lib/requests on Windows.
  • use easyinstall and run easy-install -U requests if it is installed on macOS.
  • use the native package installer from Linux by running sudo apt-get python-requests or sudo apt-get python3-requests if you’re using Python 3 on Linux.
# for Python 2 pip install requests # for python 3 pip3 install requests # if you don't have pip in your PATH environment variable & use python 2 python -m pip install requests # if you don't have pip in your PATH environment variable & use python 3 python3 -m pip install requests # for Linux only & for python 2 sudo apt-get install python-requests # for Linux only & for python 3 sudo apt-get install python3-requests
Code language: Bash (bash)

If this process does not solve your issue, explore other explanations for the error ModuleNotFoundError: No module named ‘requests’:

  • Ensure that you have installed the package for the Python version that you are using to run your script.

It’s not rare to have both Python 2 and Python 3 in a single machine; hence, you can easily get confused and install a package for the wrong version. You can run python3 show requests or python3 -m pip show requests to ensure that the library is indeed installed for the Python version you are using.

If you are working within an IDE such as Visual Studio Code, you can select the Python version you are using by opening the palette – CTRL + Shift + P or  + Shift + P on Mac):

VS Code will then list the available interpreters (including virtual environment) for you to choose from.

Remember that a package available locally won’t be available in a virtual environment. To make sure that the package is available in your virtual environment, you can run pip3 freeze > requirements.txt and check that requests is listed in the created file

  • If the requests library is listed but you still get the error

Try to uninstall the requests library and install it again. Run first pip uninstall requests (or pip3 uninstall requests) and pip install requests (or pip3 install requests). This may sound like a strange advice, but sometimes it works ;).

  • If you get an AttributeError after solving the ModuleNotFoundError

Ensure that your file is not named requests.py or that you didn’t create a variable called requests. If you do so, they shadow the module and break your code. For instance, the following code would not run properly:

#library import import requests #variable declaration requests = 1 #try to run a method linked to the library r = requests.get('https://www.google.com')
Code language: Python (python)
  • If you get an ImportError instead of an ModuleNotFoundError

These two errors belong to the same error types (based on the error hierarchy defined by Python), but:

  • ModuleNotFoundError is more specific and is triggered only if the module is not installed and available for import
  • ImportError is more generic and while the package is available, it failed to load.

Leave a Reply

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