Importerror After Successful Pip Installation

ImportError after successful pip installation

TL;DR: There are often multiple versions of python interpreters and pip versions present. Using python -m pip install <library-name> instead of pip install <library-name> will ensure that the library gets installed into the default python interpreter.

Please also note: From my personal experience I would advice against using sudo pip install to install packages into system's default python interpreter. This can lead to a various messy issues.
Whenever you are tempted to call pip with sudo, please check first if a virtualenv is not a better option for you.


Most modern systems ship multiple python interpreters. Each interpreter maintains its own set of installed packages. When installing new packages, it is important to understand into which interpreter those packages are actually installed.

On unix systems the shell can be used to understand what exactly is happening.

Typing which -a python shows all interpreters that in your PATH. The first line corresponds to the interpreter that is used when you run python from the command line.

/private/tmp/py32/bin/python
/usr/local/bin/python
/usr/bin/python

Each pip version belongs to exactly one interpreter. which -a pip shows all pip versions. Again the first line is what will be called when you type pip in your shell.

/usr/local/bin/pip
/usr/bin/python

Note that in this case python belongs to the interpreter installed in /private/tmp/py32/, but pip installs into the interpreter /usr/local/bin. After a successful install of a library, you will not be able to import it in your default python interpreter.

So how do you import the installed library?

Your first option is to start the desired interpreter with its full path. So if you type /usr/local/bin/python, you will be able to import the library.

The second - often preferred - option is to specifically invoke the right version of pip. To do so, you can use python -m pip install <library-name> instead of pip install <library-name>. This will call the pip version that belongs to your default python interpreter.

Unable to import a module that is definitely installed

In my case, it is permission problem. The package was somehow installed with root rw permission only, other user just cannot rw to it!

ModuleNotFoundError after successful pip install in Google Colaboratory

Thanks to @jojo's suggestion, I uninstalled and reinstalled the package on my local machine and was able to diagnose the problem. On both my local machine and in Colab I was able to successfully install and import the package only when including the -e (editable) flag in the pip command (pip install --user -e /content/gdrive/My\ Drive/my-package), and restarting the runtime after installation). I have no idea why including the -e flag would make a difference; please comment if you have any insight!

Why am I getting ImportError: No module named pip ' right after installing pip?

Just be sure that you have include python to windows PATH variable, then run python -m ensurepip



Related Topics



Leave a reply



Submit