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
Cmd Opens Windows Store When I Type 'Python'
How to Decode Base64 Data in Python
How to Merge a Transparent Png Image with Another Image Using Pil
Pandas: Setting No. of Max Rows
Matplotlib: Save Plot to Numpy Array
Problem Http Error 403 in Python 3 Web Scraping
Python3: Importerror: No Module Named '_Ctypes' When Using Value from Module Multiprocessing
Loop That Also Accesses Previous and Next Values
Python Round Up Integer to Next Hundred
Access Elementtree Node Parent Node
Draw a Transparent Rectangles and Polygons in Pygame
Pandas 'Count(Distinct)' Equivalent
Scraping Dynamic Content Using Python-Scrapy
Repeating Elements of a List N Times