[Fixed] ImportError: No module named requests

Problem Formulation: How to fix ImportError: No module named requests in Python?

You might be working on a web scraping project and want to import the requests library and check the status of the response. However, as soon as you try to execute your code, Python throws ImportError as shown below.

Example:

# importing the requests library
import requests

# get() Request
response = requests.get("http://pythonjobs.github.io/")

# checking response
print(response.status_code)

Output:

  ImportError: No module named requests on line 2 in main.py

✨ The Requests Library

Requests is a library in Python that allows you to send HTTP requests to a server. It returns a Response object that contains the data of the response that includes the content of the webpage, status, encoding, etc.

🐌 What is “ImportError” in Python?

📌 The import Statement

In Python, the import statement serves two main purposes:

  • It is used to search for the module specified by its name, then load and initialize it, if the module is required.
  • It additionally defines a name in the local namespace within the scope of the import statement. This local name would then be able to be used to reference the accessed module throughout the whole code.

If an import statement experiences difficulty in successfully importing a module, it raises an ImportError. Commonly, such an issue occurs because of a faulty installation or an invalid path, which will usually raise a ModuleNotFoundError in Python 3.6 and newer versions.

Example:

🐌 What does “ImportError: No module named requests” mean?

⚠️ The primary reasons behind the occurrence of this error is either a “faulty installation” of the requests module or trying to import the module before installing it.
📔 Note: Since requests is not a built-in module, you must install it before you can use it.


⚠️ Also, when there is more than one version of Python installed on the system (e.g. Python 2.x and Python 3.x simultaneously). Thus, you might have installed the package into one of the environments while you might be running your script in another environment.

Now that we know the reason behind the occurrence of this error, let’s dive into the solutions to this problem.

✨ Fixing Faulty Installation Of requests Library

You must ensure that you have installed the latest version of the requests library using the following command:

pip install requests

📁 Fixing the Path

📌 The error might persist even after you have installed the requests library. This happens because pip is installed but not in the path you can use. This means though pip is installed on your system the script is unable to locate it. Therefore, it is unable to install the library using pip in the correct path.

To fix the problem with the path follow the steps given below:

  1. Open the folder where you installed Python. A quick way to find the location of this folder is:
    • Open command prompt and type where python
2. Once you have opened the Python folder, browse and open the “Scripts” folder and copy its location. Also verify that the scripts folder has the pip file inside it.

3. Now open the Scripts directory in the command prompt using the cd command and the location that you copied previously.

4. Now install the requests library using “pip install requests” command.

After having followed the above steps, execute our script once again. And you should get the desired output.

✨ How to fix “ImportError: No module named requests” in PyCharm

If you are using an IDE like PyCharm then life becomes easy as you can eliminate the error in a flash by installing the proper version of the requests library using the following steps.

  1. Click on File and select Settings from the drop-down menu.
  2. Open Python Interpreter in the settings dialog box.
  3. Click on the ➕ icon on the side menu. This opens up a dialog box that lists the available packages.
  4. Search the requests module and click on Install Package.

Now, re-run your code and it should yield the desired output.

Recommended Course:

  • Do you want to master the most popular Python IDE fast?
  • This course will take you from beginner to expert in PyCharm in ~90 minutes.
  • For any software developer, it is crucial to master the IDE well, to write, test and debug high-quality code with little effort.
The Complete Guide to PyCharm

Join the PyCharm Masterclass now, and master PyCharm by tomorrow!

Conclusion

I hope this article helped you and answered all your queries regarding ImportError in Python.

Related Article: How to Fix “ImportError: No module named pandas”

Please subscribe to our channel and stay tuned for more interesting articles in the future. Happy learning!📚

Authors:
👨‍🎓 SHUBHAM SAYON
👩‍🎓 RASHI AGARWAL