Programming & Coding How-TosTechnology

Fix ModuleNotFoundError: No Module Named ‘requests’ in Python 3.12

Admin
Fix ModuleNotFoundError: No Module Named 'requests' in Python 3.12

Few errors stop a beginner Python script faster than ModuleNotFoundError: No module named 'requests'. You followed a tutorial, typed the import line exactly as shown, and Python still refuses to run your code. The good news is that this error rarely means anything is broken. It almost always means the requests library is not installed in the specific Python environment your script is using. In this guide you will learn why Python 3.12 shows this error, how to install the library correctly, and how to fix the trickier version of the problem where the package is installed but Python still cannot find it.

Quick Answer

The error means Python cannot find the requests library in the environment running your script. Fix it by installing the package into that exact interpreter with python -m pip install requests (or py -m pip install requests on Windows). If you use a virtual environment, activate it first. If the install succeeds but the error remains, your editor is probably using a different Python interpreter than your terminal, so select the correct one.

What This Error Actually Means

Python ships with a large standard library, but requests is not part of it. It is a third-party package that you must install separately using pip, Python’s package installer. When Python reaches the line import requests, it searches a list of folders for a module with that name. If nothing matches, it raises ModuleNotFoundError and stops the program.

The subtle part is that most computers have more than one Python setup. You might have Python 3.12 from python.org, another version bundled with an editor, and several virtual environments for different projects. Each one has its own separate set of installed packages. Installing requests into one of them does nothing for the others, and this mismatch is the number one cause of the error.

How to Fix It Step by Step

Work through these steps in order. Most people solve the problem in the first two steps.

  1. Confirm which Python is running your script. In the same terminal where you run the script, type python --version. On Windows, also try py --version. Note the version number and keep using that same terminal for the next steps.
  2. Install requests using the module form of pip. Run python -m pip install requests on macOS or Linux, or py -m pip install requests on Windows. Writing python -m pip instead of plain pip guarantees the package goes into the exact interpreter that follows it, which removes all guesswork.
  3. Activate your virtual environment first, if you use one. If your project has a virtual environment folder such as .venv, activate it before installing. On Windows PowerShell run .venv\Scripts\Activate.ps1; on macOS or Linux run source .venv/bin/activate. Your prompt should show the environment name, then repeat the install command. If you are new to this, our guide on how to create and activate a Python virtual environment on Windows 11 walks through the whole process slowly.
  4. Verify the installation. Run python -m pip show requests. You should see the package name, version, and install location. If pip reports that the package is not found, the install did not land in this interpreter.
  5. Test the import on its own. Type python -c "import requests; print(requests.__version__)". If a version number prints without an error, the terminal side is fixed and you can run your script again.
  6. Match your editor to your terminal. If the script now works in the terminal but still fails inside VS Code or another editor, the editor is using a different interpreter. In VS Code, open the Command Palette, run Python: Select Interpreter, and pick the same environment where you installed the package.

Still Not Working? Check These Causes

Your file is named requests.py

If your own script is called requests.py, Python imports your file instead of the real library and then complains in confusing ways. Rename your file to something like weather_app.py and delete any __pycache__ folder next to it before running again.

pip installed into a different Python

On systems with several Python versions, the plain pip command may belong to an older release. Compare pip --version with python --version. If they point to different versions, always use the python -m pip form so the interpreter and its installer stay matched. The official pip getting started guide explains this pairing in more detail.

The package installed for the wrong user

If you ran the install with administrator rights once and without them another time, packages can end up in different locations. Reinstall normally, without admin rights, inside your project environment. Virtual environments avoid this entire class of problem because everything lives inside the project folder.

Common Mistakes to Avoid

  • Typing pip install requests and assuming it matches your Python. The standalone pip command can belong to another installation entirely.
  • Installing while the virtual environment is inactive. The package lands in the global Python, and the project still fails.
  • Naming your script after a library. Files called requests.py, random.py, or email.py shadow the real modules.
  • Reinstalling Python completely. This almost never helps and usually creates even more interpreters to confuse things.
  • Ignoring the interpreter shown in your editor. The status bar in VS Code tells you exactly which environment will run the code.

Make the Fix Permanent for Your Project

Once the import works, spend one minute making sure it keeps working. With your environment active, run python -m pip freeze > requirements.txt. This writes a file listing every installed package with its exact version, including requests and the handful of helper packages it depends on, such as urllib3 and certifi. Anyone who clones your project — including future you, on a new laptop — can recreate the whole setup with two commands: create a virtual environment, then run python -m pip install -r requirements.txt.

This habit also protects you when you maintain several projects. Each project folder carries its own environment and its own requirements.txt, so upgrading a library for one project can never silently break another. When you return to a project after months away, activate its environment first and let the requirements file remind you what the project needs, instead of rediscovering missing modules one error at a time. Treat the first ModuleNotFoundError in any new project as a checklist reminder: environment created, environment activated, packages installed, requirements saved. Followed in that order, the error that started this guide becomes a thirty-second detour rather than an afternoon of confusion.

Conclusion

ModuleNotFoundError: No module named 'requests' is an environment problem, not a code problem. Install the package with python -m pip install requests so it lands in the interpreter that actually runs your script, activate your virtual environment before installing, and make sure your editor points at that same environment. Once those three things line up, the error disappears and stays gone.

Frequently Asked Questions

What is the difference between pip and python -m pip?

The pip command is a standalone tool that belongs to one particular Python installation. Running python -m pip tells a specific interpreter to use its own pip module, so the package is installed exactly where that interpreter looks for it. Whenever you have more than one Python version, the module form is the safer choice.

Why does my script work in the terminal but fail in VS Code?

Because the terminal and VS Code are using different Python interpreters. The terminal found the environment where requests is installed, while VS Code is set to another one. Select the correct interpreter from the Command Palette or the status bar and the error will clear.

Do I need to install requests again for every project?

Only for every virtual environment. Each environment is isolated with its own packages, which is a good thing: it stops projects from breaking each other. Global installs are shared, but best practice is one environment per project with its own requirements.txt file.

How do I install requests without an internet connection?

On a connected machine, run python -m pip download requests to save the package files, copy the folder to the offline computer, and then install with python -m pip install --no-index --find-links foldername requests. pip will resolve everything from the downloaded files.

What if pip says requests is already installed?

That means pip itself belongs to a different interpreter than the one running your script. Run python -m pip show requests with the same python command you use to launch the script. If it reports nothing, install again using that exact command and the two will finally match.

Admin

Passionate about the intersection of style and storytelling. I simplify fashion trends and industry news into easy-to-follow advice, helping you stay stylish and informed in the fast-paced world of fashion.

Leave a Reply

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