Last modified: Aug 11, 2026
Fix No module named requests in Python
Encountering the ModuleNotFoundError: No module named 'requests' is a common hurdle for Python developers. This error simply means that Python cannot find the requests library in your current environment. It's not a bug in your code, but a missing dependency. The good news is that fixing it is usually straightforward. This guide will walk you through every solution, from the simplest pip command to managing multiple environments.
Why Does This Error Occur?
Python doesn't include the requests library by default. It's a third-party package that needs to be installed separately. The error appears when you try to run import requests without having it installed in the active Python environment. This can happen if you're using a different Python interpreter than the one where the library is installed. It can also occur when a virtual environment is not activated properly.
Understanding the root cause is the first step. Often, developers have multiple Python versions on their system. Your terminal might be using Python 3.11, but you installed the library for Python 3.9. This mismatch is a frequent source of confusion. Let's look at the most reliable ways to resolve this issue.
Solution 1: Install requests Using pip
The most common fix is to install the library using pip. Open your terminal or command prompt and run the following command. This command tells the package installer to download and install the requests library from the Python Package Index (PyPI).
pip install requests
If this works, you'll see a success message. However, you might encounter a "pip not found" error. In that case, try using pip3 instead, as it's often associated with Python 3. On some systems, pip points to Python 2, which is outdated.
pip3 install requests
After the installation, verify it by running a simple Python script. You should see the version number printed without any errors.
# test_requests.py
import requests
print(requests.__version__)
# Output
2.31.0
Solution 2: Use python -m pip to Ensure Correct Environment
Sometimes, the pip command might be linked to a different Python installation than the one you're using to run your script. To guarantee you're installing for the correct interpreter, use the python -m pip syntax. This forces Python to run pip as a module, ensuring it targets the same environment as your Python command.
python -m pip install requests
If you're using Python 3 specifically, you might need to specify python3. This is especially common on Linux and macOS systems where both Python 2 and 3 are present. This method is more reliable and is considered a best practice in the Python community.
python3 -m pip install requests
This command is particularly useful when you have multiple Python versions installed. It eliminates any ambiguity about which environment is being modified. It's the first thing you should try if the simple pip install command fails.
Solution 3: Activate Your Virtual Environment
If you are using a virtual environment, it might not be activated. A virtual environment isolates your project dependencies. If it's not activated, your terminal is using the global Python environment, which may not have requests installed. To fix this, you need to activate the environment first.
On Windows, you would run the activation script from the command prompt. The exact command depends on your shell (Command Prompt or PowerShell). On macOS and Linux, you use the source command. Activating the environment changes your shell's path to point to the virtual environment's Python interpreter.
# On Windows (Command Prompt)
venv\Scripts\activate
# On Windows (PowerShell)
venv\Scripts\Activate.ps1
# On macOS/Linux
source venv/bin/activate
Once activated, you'll see the environment name in your terminal prompt, usually in parentheses. After activation, try installing requests again using the commands from Solution 1 or 2. This ensures the package is installed inside your isolated environment, not the global one.
Solution 4: Check Your IDE's Python Interpreter
If you're working in an IDE like VS Code or PyCharm, the IDE might be using a different Python interpreter than the one in your terminal. This is a very common issue. The IDE has its own setting for which Python environment to use. If you installed requests in your terminal's environment, the IDE might not see it.
In VS Code, you can check the interpreter by pressing Ctrl+Shift+P (or Cmd+Shift+P on Mac) and typing "Python: Select Interpreter". Make sure you select the same environment where you installed the library. In PyCharm, go to Settings > Project > Python Interpreter. The key is to ensure your IDE and terminal are aligned.
This is often the hidden culprit. You might have installed the package successfully in your terminal, but the IDE is still throwing the error. Always verify the interpreter path in your IDE settings. This simple check can save you a lot of time and frustration.
Solution 5: Upgrade pip and Setuptools
An outdated pip version can sometimes cause installation issues. It might not be able to resolve dependencies correctly. Upgrading pip and setuptools to their latest versions can fix underlying package management problems. This is a good preventive measure if you're facing persistent issues.
pip install --upgrade pip setuptools
After upgrading, try installing requests again. This ensures that your package manager has the latest features and bug fixes. It's a quick step that can resolve unexpected errors. Sometimes, a simple upgrade is all that's needed to clear up installation hiccups.
Solution 6: Reinstall the requests Library
If you've already installed requests but still get the error, the installation might be corrupted. A partial or broken installation can cause Python to fail to import the module. The solution is to force a reinstall. This will remove the existing package and install a fresh copy. Use the --force-reinstall flag to ensure a clean installation.
pip install --force-reinstall requests
This command ignores the cached version and downloads the package again from PyPI. It overwrites any broken files. This is a robust solution when other methods have failed. It's a good way to reset the library to a known good state.
Solution 7: Use a Requirements File
In larger projects, it's common to have a requirements.txt file. This file lists all the project dependencies. If you're cloning a repository or working on a team project, you can install all dependencies at once. This ensures that everyone has the same environment. If you see this error in a shared project, check if a requirements file exists.
pip install -r requirements.txt
This command reads the file and installs all listed packages. If requests is listed there, it will be installed. This is a best practice for managing project dependencies. It prevents the "it works on my machine" problem by standardizing the environment. For more on handling similar import errors, you might find this resource helpful: How To Solve ModuleNotFoundError: No module named in Python.
Solution 8: Check for Typos and File Names
This might seem obvious, but it's worth mentioning. If you have a file named requests.py in your project directory, Python will try to import that file instead of the installed library. This is a classic shadowing issue. Your local file takes precedence over the installed package. This will cause the import to fail or behave unexpectedly.
To fix this, rename your file to something else, like my_requests.py. Also, double-check your import statement for typos. Ensure it's import requests and not import request or import requets. A simple typo can lead to this error. This is a quick check that can save you from a wild goose chase.
Conclusion
Solving the No module named requests error is a fundamental skill for any Python developer. The issue almost always comes down to an environment mismatch or a missing installation. By following the solutions above, you can systematically diagnose and fix the problem. Start with the basic pip install requests and work your way through the more advanced checks.
Remember to always verify your virtual environment and IDE interpreter settings. These are the most common hidden causes. Using python -m pip is a reliable way to ensure you're targeting the correct Python installation. By mastering these troubleshooting steps, you'll spend less time fixing errors and more time writing great code. If you continue to face issues, remember that this error is a common part of the learning process, and every developer encounters it at some point.