Running Pip3 ImportError: cannot import name 'main'

Use python -m pip install instead of pip install

Example:

python -m pip install --user somepackage
python3 -m pip install --user somepackage

I started getting this problem after a pip upgrade:

pip install --upgrade --user pip

The pip (resp. pip3) executable is provided by your distro (python-pip package on Ubuntu 16.04).

Therefore, it is not kept up-to date with the pip package itself as you upgrade pip, and may break.

If you just use python -m pip directly, e.g. as in:

python -m pip install --user somepackage
python3 -m pip install --user somepackage

it goes through your Python path and finds the latest version of pip, and executes that file.

It relies on the fact that that file is executable, but that is a very standard type of interface, and therefore less likely to break than the hackier Debian script.

Then I recommend adding the following functions to your .bashrc:

pip() ( python -m pip "$@" )
pip3() ( python3 -m pip "$@" )

The Ubuntu 18.04 /usr/bin/pip3 file does:

from pip import main

and presumably main was removed from pip at some point which is what broke things.

The breaking pip commit appears to be: 95bcf8c5f6394298035a7332c441868f3b0169f4 "Move all internal APIs to pip._internal" which went into pip 18.0.

Tested in Ubuntu 16.04 after an update from pip3 9.0.1 to 18.0.

pyenv

Ultimately however, for serious Python development I would just recommend that you install your own local Python with pyenv + virtualenv, which would also get around this Ubuntu bug: How do I install a different Python version using apt-get?


The bug is found in pip 10.0.0.

In linux you need to modify file: /usr/bin/pip from:

from pip import main
if __name__ == '__main__':
    sys.exit(main())

to this:

from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())

numpy and scipy are in the default repositories of all currently supported versions of Ubuntu. To install numpy and scipy for Python 3.x open the terminal and type:

sudo apt update    
sudo apt install python3-numpy python3-scipy  

For Python 2.x it's:

sudo apt update  
sudo apt install --no-install-recommends python2.7-minimal python2.7 # this line is only necessary for Ubuntu 17.10 and later 
sudo apt install python-numpy # 20.04 and earlier
sudo apt install python-scipy # 18.04 and earlier

Tags:

Pip

Python3