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?
/usr/bin/pip
is provided by an Ubuntu Python package, andpip
does not overwrite it. But thepip update
is managed bypip
itself, and avoids touching Ubuntu files. – Ciro Santilli OurBigBook.com Sep 19 '19 at 06:11Debian
--The comment says:Run the main entry point, similarly to how setuptools does it, but because we didn't install the actual entry point from setup.py, don't use the pkg_resources API.
Perhaps there is a way to install Python properly in a python-way. – will Dec 10 '19 at 11:25/usr/bin/pip
does :-) – Ciro Santilli OurBigBook.com Dec 10 '19 at 13:23alias pip3='python3 -m pip'
which would also work. Using a function is fine, but has a few key differences. For example, if youexit
in a function sourced by your shell, your shell session will be terminated. – vastlysuperiorman Jan 15 '20 at 16:50