- ImportError: No module named requests
- ImportError: No module named requests
- Install Requests in OSX/Linux
- Install Requests in Windows
- How to fix ModuleNotFoundError: No module named ‘requests’
- How to reproduce the error
- How to fix this error
- Install commands for other environments
- Other common causes for this error
- Handling multiple versions of Python
- Handling Python virtual environment
- Handle IDE using a different Python version
- Conclusion
- Take your skills to the next level ⚡️
- About
- Search
- Tags
- ModuleNotFoundError: No module named ‘requests’ in Python
- Reason: “requests” Module Not Installed
- Solution: Install the “requests” Module (For Windows)
- Solution 2: Install the “requests” Module (For Linux)
- Verification of “request” Module in Python
- Conclusion
ImportError: No module named requests
In Python, if you try to import Requests without installing the module using pip, you will get ImportError: no module named requests error.
In this tutorial, let’s look at installing the Requests module correctly in different operating systems and solve no module named requests error.
ImportError: No module named requests
Requests are not a built-in module (it doesn’t come with the default python installation) in Python, you need to install it explicitly using the pip installer and then use it.
If you are getting an error installing pip checkout pip: command not found to resolve the issue.
Install Requests in OSX/Linux
The recommended way to install the requests module is using pip or pip3 for Python3 if you have pip installed already.
Using Python 2
$ sudo pip install requests
Using Python 3
$ sudo pip3 install requests
Alternatively, if you have easy_install in your system, you can install requests using the below command.
Using easy install
$ sudo easy_install -U requests
$ yum install python-requests
To install requests module on Debian/Ubuntu for Python2:
$ sudo apt-get install python-requests
And for Python3, the command is:
$ sudo apt-get install python3-requests
Install Requests in Windows
In the case of windows, you can use pip or pip3 based on the Python version you have to install the requests module.
If you have not added the pip to the environment variable path, you can run the below command in Python 3, which will install the requests module.
$ py -m pip install requests
How to fix ModuleNotFoundError: No module named ‘requests’
One error that you might encounter when working with Python is:
This error occurs when Python can’t find the requests module in your current Python environment.
This tutorial shows examples that cause this error and how to fix it.
How to reproduce the error
Suppose you want to use the Requests library to send an HTTP/HTTPS request.
You import the requests module in your code as follows:
But you get the following error when running the code:
This error occurs because the requests module is not a built-in Python module, so you need to install it before using it.
How to fix this error
To resolve this error, you need to install the requests library using pip as shown below:
Once the module is installed, you should be able to run the code that imports requests without receiving this error.
Install commands for other environments
The install command might differ depending on what environment you used to run the Python code.
Here’s a list of common install commands in popular Python environments to install the requests module:
Once the module is installed, you should be able to run the code without receiving this error.
Other common causes for this error
If you still see the error even after installing the module, it means that the requests module can’t be found in your Python environment.
- You may have multiple versions of Python installed on your system, and you are using a different version of Python than the one where Requests is installed.
- You might have Requests installed in a virtual environment, and you are not activating the virtual environment before running your code.
- Your IDE uses a different version of Python from the one that has Requests
Let’s see how to fix these errors in practice.
Handling multiple versions of Python
If you have multiple versions of Python installed on your system, you need to make sure that you are using the specific version where the Requests module is available.
You can test this by running the which -a python or which -a python3 command from the terminal:
In the example above, there are two versions of Python installed on /opt/homebrew/bin/python3 and /usr/bin/python3 .
Suppose you run the following steps in your project:
- Install Requests with pip using /usr/bin/ Python version
- Install Python using Homebrew, you have Python in /opt/homebrew/
- Then you run import requests in your code
The steps above will cause the error because Requests is installed in /usr/bin/ , and your code is probably executed using Python from /opt/homebrew/ path.
To solve this error, you need to run pip install requests command again so that Requests is installed and accessible by the new Python version.
Next, you can also have Requests installed in a virtual environment.
Handling Python virtual environment
Python venv package allows you to create a virtual environment where you can install different versions of packages required by your project.
If you are installing requests inside a virtual environment, then the module won’t be accessible outside of that environment.
You can see if a virtual environment is activated or not by looking at your command prompt.
When a virtual environment is activated, the name of that environment will be shown inside parentheses as shown below:
In the picture above, the name of the virtual environment (base) appears when the Conda virtual environment is activated.
You need to turn off the virtual environment so that pip installs to your computer.
When your virtual environment is created by Conda, run the conda deactivate command. Otherwise, running the deactivate command should work.
Handle IDE using a different Python version
Finally, the IDE from where you run your Python code may use a different Python version when you have multiple versions installed.
For example, you can check the Python interpreter used in VSCode by opening the command palette ( CTRL + Shift + P for Windows and ⌘ + Shift + P for Mac) then run the Python: Select Interpreter command.
You should see all available Python versions listed as follows:
You need to use the same version where you installed Requests so that the module can be found when you run the code from VSCode.
Once done, you should be able to import Requests into your code.
Conclusion
In summary, the ModuleNotFoundError: No module named ‘requests’ error occurs when the requests library is not available in your Python environment. To fix this error, you need to install requests using pip .
If you already have the module installed, make sure you are using the correct version of Python, deactivate the virtual environment if you have one, and check for the Python version used by your IDE.
By following these steps, you should be able to import the requests module in your code successfully.
I hope this tutorial is helpful. Until next time! 👋
Take your skills to the next level ⚡️
I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!
About
Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.
Search
Type the keyword below and hit enter
Tags
Click to see all tutorials tagged with:
ModuleNotFoundError: No module named ‘requests’ in Python
In Python, the web library named “requests” is utilized to exchange requests on the web. The “requests” library provides human-friendly and simple HTTP requests. To use “requests” in Python script, the module must be imported at the program’s start using the keyword “import”. In Python, the error “ModuleNotFoundError” occurs when the “requests” module is imported without being installed.
This article will demonstrate various reasons and solutions for “ModuleNotFoundError: no module named request” in Python using the following aspects:
Reason: “requests” Module Not Installed
Primarily, this error occurs when the user tries to import the “requests” module without installing it into the Python environment. The error is displayed in the following snippet:
The above snippet shows that the “requests” module is not installed in our system.
Solution: Install the “requests” Module (For Windows)
To resolve this error, the “requests” module must be installed in Python. Here are the steps you need to follow:
Step 1: Open Command Prompt Terminal (CMD)
The first step is to open the command prompt terminal of your window by following the below snippet:
Step 2: Install the “requests” Module Using the pip
After opening the command prompt terminal, type the given below command and press enter to execute:
The “requests” module is successfully installed in Python, as shown in the above output.
If you get a permission error during installation, then use the given below command:
> pip install requests --user
To install the “requests” module in Anaconda and Jupyter Notebook, use the given below command in their respective environment:
# for Anaconda conda install -c anaconda requests # for Jupyter Notebook !pip install requests
Note: if the module is installed in the wrong environment. If you encounter the “ModuleNotFoundError”, you can follow these above steps by choosing the correct environment.
Solution 2: Install the “requests” Module (For Linux)
To install the “request” module in Linux using the “pip” package manager, you first need to install “pip” if you don’t already have it in the system, click here to read out the installation of pip on Linux:
After installing the Python “pip” package manager now you can type the given below command in terminal:
Verification of “request” Module in Python
To check whether the “request” module is successfully installed in our system or not type the given below command in the respective shell:
The above snippet shows the “requests” module version and summary that is installed in Python.
Conclusion
In Python, the “ModuleNotFoundError: No module named requests” occurs when a user tries to import the “requests” module in the program without installing it. To resolve this error, install the “requests” module using the pip package manager. After that, import the “requests” module, and the error will be resolved. This write-up has delivered different reasons and solutions for the “No module named request” error in Python.