[Solved] ModuleNotFoundError: No module named ‘requests’ in Python

The Python “ModuleNotFoundError: No module named ‘requests'” happens when we neglect to install the solicitations module prior to bringing it in or installing it in the wrong climate. To address the mistake, introduce the module by running the pip to install the request command.

Open your terminal in your undertaking’s root registry and introduce the solicitations module.

# in a virtual environment or using Python 2
pip install requests

# for python 3 (could also be pip3.10 depending on your version)
pip3 install requests

# if you get permissions error
sudo pip3 install requests

# if you don't have pip in your PATH environment variable
python -m pip install requests

# for python 3 (could also be pip3.10 depending on your version)
python3 -m pip install requests

# alternative for Ubuntu/Debian
sudo apt-get install python3-requests

# alternative for CentOS
sudo yum install python-requests

# for Anaconda
conda install -c anaconda requests

After you install the requests package, have a go at bringing in it like:

In main.py

In main.py

import requests

def make_request():
    res = requests.get('https://reqres.in/api/users')

    print(res.json())


make_request()

The Python error “ModuleNotFoundError: No module named ‘requests'” happens because of several factors:

  1. Not having the requests package installed by running pip install request.
  2. Introducing the package in an alternate Python form/version than the one you’re utilizing.
  3. Introducing the package all around/globally the world and not in your virtual climate.
  4. Your IDE running an inaccurate variant of Python.
  5. Naming your module requests.py which would shadow the authority module.
  6. Proclaiming a variable named request which would shadow the imported variable.

Assuming the error perseveres, get your Python rendition and ensure you are installing the package utilizing the right Python adaptation.

In the shell

python --version

For instance or example, my Python variant is 3.10.4, so I would introduce the requests package with pip3.10 install requests.

In shell

pip3.10 install requests

# if you get permissions error use pip3 (NOT pip3.X)
sudo pip3 install requests

Notice that the rendition number relates to the variant of pip I’m utilizing.

On the off chance that the PATH for pip isn’t set up on your machine, supplant pip with python3 – m pip:

In shell

# make sure to use your version of Python, e.g. 3.10
python3 -m pip install requests

On the off chance that the “No module named ‘requests'” mistake perseveres, have a go at restarting your IDE and development server/script.

You can check to assume you have the requests package installed by running the pip show requests command.

In shell

# check if you have requests installed
pip3 show requests

# if you don't have pip setup in PATH
python3 -m pip show requests

The pip show requests command will either express that the package isn’t introduced or show a lot of data about the bundle, including where the package is introduced.

On the off chance that the package isn’t installed, ensure your IDE is utilizing the right variant of Python.

  • In the event that you have numerous Python forms installed on your machine, you could have installed the requests package utilizing the incorrect version or your IDE may be arranged to utilize an alternate rendition.

For instance, In VSCode, you can press CTRL + Shift + P or (⌘ + Shift + P on Mac) to open the command range.

Then, at that point, type “Python select interpreter” in the field.

Then select the right python form starting from the drop menu.

  • Your IDE ought to utilize a similar form of Python (counting the virtual climate) that you are utilizing to introduce bundles from your terminal.
In shell

# use correct version of Python when creating VENV
python3 -m venv venv

# activate on Unix or MacOS
source venv/bin/activate

# activate on Windows (cmd.exe)
venv\Scripts\activate.bat

# activate on Windows (PowerShell)
venv\Scripts\Activate.ps1

# install requests in virtual environment
pip install requests

Your virtual climate will utilize the variant of Python that was utilized to make it.

  • Assuming the error persists, ensure you haven’t named a module in that project of mind as requests.py in light of the fact that that would shadow the first requests module.

You likewise ought not to be pronouncing variable named demands as that would likewise shadow the first module.

In the event that the error isn’t settled, attempt to uninstall the solicitations package and afterward install it.

In shell

# check if you have requests installed
pip3 show requests

# if you don't have pip setup in PATH
python3 -m pip show requests

# uninstall requests
pip3 uninstall requests

# if you don't have pip setup in PATH
python3 -m pip uninstall requests

# install requests
pip3 install requests

# if you don't have pip setup in PATH
python3 -m pip install requests

Take a stab at restarting your IDE and development server/script.

You can likewise attempt to overhaul the form of the requests package.

In shell

pip3 install requests --upgrade

# if you don't have pip setup in PATH
python3 -m pip install requests --upgrade
  • In the event that the mistake continues, I would propose watching a speedy video on the best way to involve Virtual conditions in Python.

For ‘Windows’ Operating System:

For ‘Mac’ and ‘Linux’ Operating systems:

Thank you..!

Ayush
Ayush

Leave a Reply

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