Using pip3 – module “importlib._bootstrap” has no attribute “SourceFileLoader”

Using pip3 – module “importlib._bootstrap” has no attribute “SourceFileLoader” is an error which usually occurs in Python because you have installed your version of python and pip3 from different sources/origins.

In this article I am going to try solving the error while trying to explain why the error is here, I want to also introduce some solutions which have successfully worked for other fellow developers.

Explaining the Error : module “importlib._bootstrap” has no attribute “SourceFileLoader”

The error can happen literally to anyone who has Python, so watch your back LoL … usually most people encounter this error when they install Python and pip3 from different origins. But there are other cases and reasons that could cause this.

Here is an example of the message which pops-up with the error.

                                                                       #
File "/usr/lib/python3/dist-packages/pkg_resources.py", line 1479, in <module>
 register_loader-type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module "importlib._bootstrap" has no attribute "SourceFileLoader"
                                                                       #

For example, You can reproduce the error by running any pip3 installation command like the one bellow only after installing pip and pip3.

                                                                       #
pip3 install matplotlib
                                                                       #

Bellow I give my best shot at solving this error and I will also present other possible solutions which have worked for other developers.

Solution 1 : Install pip with python’s built-in facility

One of the tricks you can try in order to solve this error is to use python’s built-in facility to install pip. The goal is to solve the confusion that happens since pip3 and python are installed from different origins.

In order to do that please run the following command.

                                                                       #
python3 -m ensurepip --upgrade
                                                                       #

If this worked, congrats, if it did not try the solution bellow.

Solution 2 : upgrade setuptools

Another solution is a solution for windows and Ubuntu. The solution is to upgrade the setuptools. You can upgrade using the command below.

                                                                       #
pip install setuptools --upgrade
                                                                       #

Solution 3 : You wont Believe it

If that does not work, please see if you are trying to install the correct package, sometimes people run

                                                                       #
pip install python-packagename
                                                                       #

Instead of running

                                                                       #
pip install packagename
                                                                       #

Please try this solution you might think I am crazy but just try it.

If the solutions above helped you, consider supporting me on Kofi, any help is appreciated. Big Red Donation button is at the top of this article

Summing-up

This is the end of our blog post, I hope this has been helpful and helped you fix the issue or at least pointed you in the right way.

Python is fun, keep writing coding, cheers.

Learn more about Python, check out the Python Documentation : https://docs.python.org/3/