How to fix ModuleNotFoundError: No module named 'requests'

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to fix ModuleNotFoundError: No module named 'requests' error, and what causes it.

Why does this error happen?

Traceback (most recent call last):
  File "/home/owlhowto/Desktop/app.py", line 3, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

This error happen because you are trying use the module requests without installing it first on your machine.

This error happens also in cases when module is not being used but it's only imported on your code, so even if you import the requests module without using it, you will still encounter the error "ModuleNotFoundError: No module named 'requests'"

This error can be fixed easily by following the steps below.

Why does ModuleNotFoundError: No module named 'requests' error happen

Like we said this error happens in two cases

1) This error happens when the module requests is imported in your code but it's not installed on your system

#!/usr/bin/env python3

import requests

2) This error also happens when the module requests is imported and also being used in your code, but it's not installed on your system.

#!/usr/bin/env python3

import requests

...

So even if you are not using the module requests, but you are importing it on your code, then you will still encounter the error ModuleNotFoundError: No module named 'requests', when running your code.

How to fix ModuleNotFoundError: No module named 'requests'

This error can be easily fixed by installing the requests module.

The requests module can be installed via different methods. However, in this tutorial, we'll be installing requests by using pip.

To install requests via pip, simply run the command below.

pip install requests

Once you install the requests module, you should be able to use it, and run your python code without any issue.

For example, in our case we were trying to run a python code that shows the version of the requests module

#!/usr/bin/env python3

import requests

show_requests_version = requests.__version__

print(show_requests_version)

As you can see we are not getting the error ModuleNotFoundError: No module named 'requests' anymore when using the requests module.

Conclusion

In this tutorial, you learned that if you are using the requests module on your code without first installing it on your machine, once you run your code you encounter the error ModuleNotFoundError: No module named 'requests'.

We also showed how to fix this error by installing the requests module on your system.

By now you should be able to use the requests module on your code.