Requests is not a built in module (does not come with the default python installation), so you will have to install it:
OSX/Linux
Python 2: sudo pip install requests
Python 3: sudo pip3 install requests
if you have pip
installed (pip
is the package installer for python and should come by default with your python installation).
If pip is installed but not in your path you can use python -m pip install requests
(or python3 -m pip install requests
for python3)
Alternatively you can also use sudo easy_install -U requests
if you have easy_install
installed.
Linux
Alternatively you can use your systems package manager:
For centos: sudo yum install python-requests
For Debian/Ubuntu Python2: sudo apt-get install python-requests
For Debian/Ubuntu Python3: sudo apt-get install python3-requests
Windows
Use pip install requests
(or pip3 install requests
for python3) if you have pip
installed and Pip.exe added to the Path Environment Variable. If pip is installed but not in your path you can use python -m pip install requests
(or python3 -m pip install requests
for python3)
Alternatively from a cmd prompt, use > Patheasy_install.exe requests
, where Path
is your Python*Scripts
folder, if it was installed. (For example: C:Python32Scripts
)
If you manually want to add a library to a windows machine, you can download the compressed library, uncompress it, and then place it into the Libsite-packages
folder of your python path. (For example: C:Python27Libsite-packages
)
From Source (Universal)
For any missing library, the source is usually available at https://pypi.python.org/pypi/. You can download requests here: https://pypi.python.org/pypi/requests
On mac osx and windows, after downloading the source zip, uncompress it and from the termiminal/cmd run python setup.py install
from the uncompressed dir.
(source)
One error that you might encounter when working with Python is:
ModuleNotFoundError: No module named 'requests'
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:
import requests
r = requests.get('https://example.com/api')
print(r.status_code)
But you get the following error when running the code:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
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:
pip install requests
# For pip3:
pip3 install requests
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:
# if you don't have pip in your PATH:
python -m pip install requests
python3 -m pip install requests
# Windows
py -m pip install requests
# Anaconda
conda install -c anaconda requests
# Jupyter Notebook
!pip install requests
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.
There are several reasons why this error can happen:
- 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:
$ which -a python3
/opt/homebrew/bin/python3
/usr/bin/python3
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! 👋
Couple days ago i installed a clean Python3 environment using MiniConda. After launching Jupyter Lab and trying to start working with the requests module, i got an import error.
In this post i will show you how to easily troubleshoot requests import errors in your Python development environment of choice, being it Jupyter, PyCharm, Spyder etc’ and distribution.
Troubleshooting the no module named requests error
Using pip
If you are installing your Python packages manually, not using Anaconda / MiniConda or other prepackaged distribution, it’s easily to use the Python package management utility (PIP) to solve this issue. I’ll go through the steps in a Windows computer, those will be pretty quite similar in macOS or Linux distributions.
- First, go ahead and close your Python editor environment.
- Then open your command line ( Windows: Start >> run >> cmd) or Terminal.
- Now type the following command:
pip install requests
- Next, go ahead and close your command or Terminal window.
- Now go ahead and reopen your Python editor and import requests.
import requests as re
Using MiniConda
If you are using MiniCond then follow these steps:
- Close your Python editor.
- Hit the Windows icons and type Anaconda Prompt. and hit Enter.
- Then type: conda activate <your_environment_path> and hit Enter. As an example, you’ll type C:envsPython3.81
- Then type pip install requests and Enter.
- Re-open your Python Editor and keep coding.
No module named requests – even after i pip install
Issue: you are still getting the import error even after executing the installation using pip or the Anaconda Environment Manager.
Solution: ensure that you have indeed closed your Python editor before importing the package. If still you get and error, reboot your Windows PC and try again.
Warning / Errors when trying to pip install requests
Issue: When trying to import the requests module from the Python package repository, you encounter errors:
ERROR: Could not find a version that satisfies the requirement requests (from versions: none)
Solution: most probably you have an issue with your Internet connection. Ensure that you are online and try again.
The modulenotfounderror no module named ‘requests‘ Windows code exception happens when the system misses the requests modules from the pip install commands. As a result, the application confirms the inconsistencies with the module named ‘requests’ file by displaying virtual environment error logs.
In addition, you will likely encounter a similar modulenotfounderror: no module named ‘requests’ Python3 warning when you install requests and packages in a different Python version than the one you are using to compile the module named project.
Therefore, we researched and wrote a sophisticated debugging guide to help you repair the module named requests using standard techniques that remove the modulenotfounderror: no module named ‘requests’ Mac error log.
Contents
- Why Is the Modulenotfounderror No Module Named Requests Bug Happening?
- – Messing up the Pip Functions and Commands
- – Installing Requests in a Different Python Version
- How to Repair the No Module Named Requests Code Exception?
- – Altering the Inputs in the Root Directory
- – Fix the Path via the Command Prompt
- Conclusion
Why Is the Modulenotfounderror No Module Named Requests Bug Happening?
The modulenotfounderror no module named ‘requests’ PyCharm bug happens when the application misses the named ‘requests’ from the pip commands. However, your module named requests will likely malfunction when installing the import requests in a different Python version than the one you are using to compile the project.
Therefore, the modulenotfounderror no module named ‘requests’ Jupyter Notebook code exception is inevitable when misconfiguring the pip commands for the named requests. For instance, a single flawed command line could affect the entire project, although most named requests raise no warnings and are fully functional.
Nevertheless, we suggest scanning the modulenotfounderror: no module named ‘requests’ Jenkins error log before implementing the debugging techniques and repairing the misconfigured inputs. Taking this into consideration, we will teach you how to replicate and reproduce the code exception using common elements and pip commands, which may resemble the properties of your project or application.
On the contrary, the modulenotfounderror no module named ‘requests’ vscode message sometimes indicates you are using an incorrect Python version when installing the packages. In addition, introducing the properties globally and not in the virtual environment confuses the system because it cannot locate the necessary functions.
Henceforth, opening the terminal and altering the values in the root directory is sometimes essential to fix the issue, but we will wait to discuss the possible debugging techniques. Namely, we will explore and exemplify the error’s culprits and causes, which should help you pinpoint the failed code snippet or pip command.
– Messing up the Pip Functions and Commands
We confirmed the modulenotfounderror no module named ‘requests’ but requests is installed error log when several pip properties malfunction. In return, the system displays a flawed message indicating the deviations confirming the failed code line. Remember that a single project could render many pip commands and properties.
You can learn more about these inputs in the following example:
> absl-py == 0.13.0
> account == 0.2.0
> action == 1.4.6
> adder == 1.2
> aiohttp == 3.0.4.post0
> airtest == 1.2.8
> alembic == 1.7.2
> altgraph == 0.22
> amazoncaptcha == 0.7.6
> amqp == 2.6.1
> androidemu == 1.0.2
> ansicon == 1.97.0
> anticaptchaofficial == 1.7.51
> anyio == 3.9.1
> appdirs == 1.6.4
> Appium-Python-Client == 2.6.3
> apptools == 5.1.0
> APScheduler == 3.6.3
> asgiref == 3.5.2
> astunparse == 1.6.3
> async-generator == 1.10
> async-timeout == 4.0.2
> atomicwrites == 1.4.0
> attmap == 0.13.0
> attrs == 21.2.0
> AuthGG == 0.9
> Babel == 2.10.1
> backcall == 0.2.0
> bcrypt == 3.2.2
> bearer == 3.1.2
> beautifulsoup4 == 4.5.3
> billiard == 2.6
After running these commands, the invalid chapter is only complete after listing the visual output the machine throws.
The following example provides the message:
Traceback (most recent call last):
File “C:UsersAppDataLocalTempONEFIL1constanttemp.py”, line 22, in <module constanttemp>
import pyrebase
File “C:UsersAppDataLocalTempONEFIL1pyrebase__init__.py”, line 98, in <module pyrebase>
File “C:UsersAppDataLocalTempONEFIL1pyrebasepyrebase.py”, line 22, in <module pyrebase.pyrebase>
ModuleNotFoundError: No module named ‘requests.packages.urllib3.contrib.appengine’
However, other confusing instances and broken procedures exist that display a similar code exception and terminate your project.
– Installing Requests in a Different Python Version
Your program or application will likely experience similar module code exceptions when installing or uninstalling requests in a different Python version than the one you are using. Namely, the programs and commands must comply with your operating system when creating advanced scripts. Thus, we will exemplify the PyCache libraries that fail to complete the procedure and ruin your document.
The following code snippet provides the complete syntax:
Installing requests-2.26.0:
/usr/ local/ lib/ python3.5/ site-packages/ requests-2.26.0.dist-info/ DESCRIPTION.rst
/usr/ local/ lib/ python3.5/ site-packages/ requests-2.26.0.dist-info/ INSTALLER
/usr/ local/ lib/ python3.5/ site-packages/ requests-2.26.0.dist-info/ LICENSE.txt
/usr/ local/ lib/ python3.5/ site-packages/ requests-2.26.0.dist-info/ METADATA
/usr/ local/ lib/ python3.5/ site-packages/ requests-2.26.0.dist-info/ RECORD
/usr/ local/ lib/ python3.5/ site-packages/ requests-2.26.0.dist-info/ WHEEL
/usr/ local/ lib/ python3.5/ site-packages/ requests-2.26.0.dist-info/ metadata.json
/usr/ local/ lib/ python3.5/ site-packages/ requests-2.26.0.dist-info/ top_level.txt
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __init__.py
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ __init__.cpython-36.pyc
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ __version__.cpython-36.pyc
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ _internal_utils.cpython-36.pyc
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ adapters.cpython-36.pyc
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ api.cpython-36.pyc
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ auth.cpython-36.pyc
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ certs.cpython-36.pyc
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ compat.cpython-36.pyc
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ cookies.cpython-36.pyc
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ exceptions.cpython-36.pyc
/usr/ local/ lib/ python3.5/ site-packages/ requests/ __pycache__/ help.cpython-36.pyc
Although we could list the pip commands, we kept the example as short as possible because they are irrelevant when using different Python versions. Still, this guide’s debugging methods apply to all operating systems and programs.
How to Repair the No Module Named Requests Code Exception?
You can repair the no module named request code exception by installing the adequate library requests with pip install privileges. In addition, we suggest altering the inputs in your root directory to provide the correct request module. We will also teach you how to fix the flawed path.
Nevertheless, your primary debugging approach should be installing the library requests with pip install privileges. Two code snippets exist, which apply to all operating systems and versions.
The following approach uses a standard pip command line:
Collecting requests
Downloading https://files.pythonhosted.org/packages/45/1e/0c169c6a5381e241ba7404532c16a21d86ab872c9bed8bdcd4c423954103/requests-2.24.0-py2.py3-none-any.whl (61kB)
100% | 71kB 1.8MB/s
Collecting certifi>=2017.4.17 (from requests)
Downloading https://files.pythonhosted.org/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58kB)
100% | 61kB 4.7MB/s
Collecting chardet<4,>=3.0.2 (from requests)
Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
100% | 143kB 4.1MB/s
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests)
Downloading https://files.pythonhosted.org/packages/56/aa/4ef5aa67a9a62505db124a5cb5262332d1d4153462eb8fd89c9fa41e5d92/urllib3-1.25.11-py2.py3-none-any.whl (127kB)
100% | 133kB 4.7MB/s
Installing collected packages: certifi, idna, chardet, urllib3, requests
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.11
The last code line confirms the successful procedure and reenables all functions. You can also use the following technique to achieve the same results:
Collecting requests
Downloading https://files.pythonhosted.org/packages/45/1e/0c169c6a5381e241ba7404532c16a21d86ab872c9bed8bdcd4c423954103/requests-2.24.0-py2.py3-none-any.whl (61kB)
100% | 71kB 2.0MB/s
Collecting certifi>=2017.4.17 (from requests)
Downloading https://files.pythonhosted.org/packages/5e/c4/6c4fe722df5343c33226f0b4e0bb042e4dc13483228b4718baf286f86d87/certifi-2020.6.20-py2.py3-none-any.whl (156kB)
100% | 163kB 1.9MB/s
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests)
Downloading https://files.pythonhosted.org/packages/56/aa/4ef5aa67a9a62505db124a5cb5262332d1d4153462eb8fd89c9fa41e5d92/urllib3-1.25.11-py2.py3-none-any.whl (127kB)
100% | 133kB 4.3MB/s
Collecting chardet<4,>=3.0.2 (from requests)
Downloading https://files.pythonhosted.org/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58kB)
100% | 61kB 5.7MB/s
Installing collected packages: certifi, urllib3, chardet, idna, requests
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.11
We advise using the following debugging method if the error persists and your project experiences similar obstacles.
– Altering the Inputs in the Root Directory
Another tested and confirmed solution requires changing the inputs in the root directory. This script installs the module by running the pip request command snippet. However, since it differs for all programs and applications, we will provide several inputs with comments to help you understand the purpose. Hence, you can copy and paste the necessary syntax in your document to overcome the error log.
The following section teaches how to change the properties in the root directory:
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
pip install requests –user
# 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
# using py alias (Windows)
py -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
# for Jupyter Notebook
!pip install requests
Pay attention to the version and dependency before selecting the option from this code snippet.
– Fix the Path via the Command Prompt
This guide’s last debugging technique teaches how to fix the path via the command prompt. The following list provides the necessary steps:
- Open the command field and type the following input: where python.
- Browse the scripts folder and copy its location. Ensure the folder contains the pip file.
- Open the directory via the command prompt using this command: cd.
- Install the adequate library using the pip install requests command.
The system should confirm the procedure encounters no issues or warnings after completing this procedure. You can repeat this technique for all broken modules.
Conclusion
The no module requests PyCharm bug happens when the application misses the named inputs from the pip commands. We explained the following points so you don’t have to think about the module error anymore:
- You will encounter a similar warning when you install requests and packages in a different Python version
- We reproduced the error log by messing up the pip functions and commands
- Installing adequate library requests with pip install privileges is an excellent debugging approach
- The second solution suggests changing the inputs in the root directory
Python errors and broken messages are dynamic and require attention when dealing with them to avoid further complications. Hence, reading this guide is the most optimal way of discovering the best solutions.
- Author
- Recent Posts
Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team