Troubleshooting "ModuleNotFoundError: No module named 'requests'"

Feb 3, 2024 ยท 2 min read

It's frustrating when you try to import requests in your Python code and get hit with the dreaded ModuleNotFoundError. This common error essentially means Python can't find the requests module. Don't worry - with a few easy checks, you can get imports working smoothly.

First, make sure requests is actually installed in your environment. Open a Python shell and try:

import pkg_resources

If it succeeds, requests is installed. If not, you'll see another ModuleNotFoundError. In that case, install requests with:

pip install requests

With that done, why would you still get errors importing it? There are a couple reasons.

Using multiple Python versions. If you installed requests for Python 3 but try to import it in Python 2, you'll likely hit import issues. Check that your environments and interpreters match up.

Virtual environments. If you use virtual environments, requests will only be available in the env it's installed in. Activate the proper env before trying to import.

Module name conflicts. An unlikely possibility is you have another module named requests that conflicts with the one you want. Try import requests as req to rule out any naming issues.

Finally, the problem could come down to your Python path. Call sys.path - that shows the directories Python searches through when resolving imports. Ensure it includes your site-packages folder where requests should live.

I know import issues can be unintuitive at first, but once you get the hang of Python environments and paths, it clicks. Start by verifying requests installed properly for the interpreter you're calling it from. Make sure to activate virtual environments when necessary. And don't hesitate to double check module names and Python version if you still get stumped!

Browse by tags:

Browse by language:

Tired of getting blocked while scraping the web?

ProxiesAPI handles headless browsers and rotates proxies for you.
Get access to 1,000 free API credits, no credit card required!