pip: pip v10 breaks Debian/Ubuntu pip3 command
Maintainer note: Anyone that still gets this issue please see #5599.
- Pip version: 10.0.0
- Python version: 3.5.2
- Operating system: Ubuntu 16.04 (EDIT: tested on
debian:9.4
too, same thing happens)
Description:
When upgrading pip (to v10) on at least Ubuntu 16.04, the pip3
command stops working (“cannot import main”, see below). This is on a fresh install.
What I’ve run:
(Note that I’ve stripped out all the apt output etc., since I think it’s not needed here. Let me know if you still want it!)
me@host$ sudo docker run -it ubuntu:xenial
root@container# apt update && apt install python3-pip
root@container# pip3 --version
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)
root@container# pip3 install --upgrade pip
Collecting pip
Downloading pip-10.0.0-py2.py3-none-any.whl (1.3MB)
100% |################################| 1.3MB 1.4MB/s
Installing collected packages: pip
Found existing installation: pip 8.1.1
Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
Successfully installed pip-10.0.0
root@container# pip --version
pip 10.0.0 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)
root@container# pip3 --version
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ImportError: cannot import name 'main'
root@container# cat /usr/bin/pip3
#!/usr/bin/python3
# GENERATED BY DEBIAN
import sys
# 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.
from pip import main
if __name__ == '__main__':
sys.exit(main())
Not sure if this is something that should be fixed on the pip side or on the Debian side.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 25
- Comments: 42 (14 by maintainers)
Links to this issue
- python - Error after upgrading pip: cannot import name 'main' - Stack Overflow
- python - File "/usr/bin/pip", line 9, in <module> from pip import main ImportError: cannot import name main - Stack Overflow
- "ImportError: cannot import name main" after upgrading to pip 10.0.0 for Python version 2.7.12 - Only one version of Python is installed - Stack Overflow
- Need recommendations on which distro to use.
Commits related to this issue
- Don't update system pip by pip See https://github.com/pypa/pip/issues/5221 — committed to BrainTwister/docker-devel-env by BerndDoser 6 years ago
- use python -m pip instead of pip see https://github.com/pypa/pip/issues/5221 — committed to gsemet/python-module-cookiecutter by gsemet 6 years ago
- Don't try to upgrade pip See https://github.com/pypa/pip/issues/5221 — committed to canonical/dotrun-image by nottrobin 6 years ago
- Don't update system pip by pip See pypa/pip#5221 — committed to BrainTwister/docker-devel-env by BerndDoser 6 years ago
- Use workaround for upgrading pip See https://github.com/pypa/pip/issues/5221 — committed to BrainTwister/docker-devel-env by BerndDoser 6 years ago
- Fix pip 10 errors and directory error pip3 no longer works when updated (see https://github.com/pypa/pip/issues/5221). Also, git clone should create the right folder (or you need to mkdir it first) — committed to mdchia/NCI_setup_help by mdchia 6 years ago
- Avoid known issue with pip 10.0.x See https://github.com/pypa/pip/issues/5221#issuecomment-382069604 Signed-off-by: Benoit Donneaux <benoit.donneaux@digital-me.nl> — committed to dappre/indy-anoncreds by btlogy 6 years ago
- Avoid known issue with pip 10.0.x See https://github.com/pypa/pip/issues/5221#issuecomment-382069604 Signed-off-by: Benoit Donneaux <benoit.donneaux@digital-me.nl> — committed to dappre/indy-anoncreds by btlogy 6 years ago
- Avoid known issue with pip 10.0.x See https://github.com/pypa/pip/issues/5221#issuecomment-382069604 Signed-off-by: Benoit Donneaux <benoit.donneaux@digital-me.nl> — committed to dappre/indy-anoncreds by btlogy 6 years ago
- Added fix for pip issue https://github.com/pypa/pip/issues/5221#issuecomment-382069604 — committed to Harmonic/laradock by deleted user 6 years ago
- Update install_software.sh echo "pip3 install --upgrade pip" would not upgrade pip, removing echo and quotations — committed to tomas-fer/HybPhyloMaker by tomas-fer 6 years ago
We solved this issue by clear hash in bash:
Or in dash (sh):
Also hit this issue while building docker images.
@RonnyPfannschmidt says “replacing the system pip using pip is always an act of system-vandalism where the one inflicting it is responsible for the fallout”, which has 6 thumbs up. I find this to be an especially obtuse comment given that I instructed to do so by pip itself:
You are using pip version 8.1.1, however version 10.0.0 is available. You should consider upgrading via the ‘pip install --upgrade pip’ command.
If there is some validity to that comment, then the creators of pip should remove this message and I would encourage @RonnyPfannschmidt to raise an issue to that effect and make his case.
on extra note - replacing the system pip using pip is always an act of system-vandalism where the one inflicting it is responsible for the fallout
Thanks, hadn’t realized that replacing the system pip was a bad idea but it makes sense. However, it would be good UX if pip would not nag about upgrading in that case. Would that be possible? I reckon a lot of people (including me) would just do whatever “the thing” asks them to do.
It works for me: curl https://bootstrap.pypa.io/get-pip.py | sudo python
First, some notes about what happened here on Debian/Ubuntu (and likely a few more Linux Distros):
This issue is caused by, well, both being violated in a way.
pip install --upgrade pip
, as root, without any other parameters modifies files that are supposed to managed by apt, which breaks the script by Debian.Some general tips on Linux:
It’s a good habit to use
--user
whenever outside a venv.Never run pip with sudo unless you know what you’re doing.
What’s the workaround?
@standag’s solution is useful when this is caused by bash’s caching of executables.
If you’ve modified your OS package manager’s installation of pip (eg by using
sudo pip
) andpython -m pip
is still working, one workaround is to uninstall the pip installed version and reinstall the package manager installed version.If you’re not on Debian/Ubuntu and pip broke for you, try running:
If the above this doesn’t resolve your problems, please file a new issue.
[edited by @pradyunsg: make it more relevant for linking everyone with similar issues to this comment; update the suggestion to include uninstalling/reinstalling workaround]
I’m sorry, but I’d like to point out that running python code curl’d from some website with root access is just terrifyingly insecure.
Convince the debian/ubuntu people to not vendor and then let rot half their packages, and that’d be a valid argument.
@RonnyPfannschmidt
is a comment of mental vandalism. As if the person doing the (naive) upgrade is intentionally setting out to damage their own installation… If that were the case, then pip itself shouldn’t be nagging the user to upgrade from 9.0.1 to 10.0.1 with every single pip command executed. I myself followed that recommendation and ended up in this mess.
Fortunately:
sudo python -m pip install pip==9.0.1
was an easy enough remedy.Blaming the victim is no answer, though.
How to reproduce this under a fresh Ubuntu 17.10 installation, including the evidence that the commands in comment 5221 fail to fix it, and what I proposed does fix it.
Installation of both pips (system and user), which breaks the pip command:
As you can see, the
pip
command points to the system pip by default, not the user installed one.Commands from referenced comment, evidence of them not fixing the problem:
As you can see, as long as both pips are installed and the
pip
command points to the system pip (default behavior in Ubuntu), the problem will persist.Fix option 1:
Remove system pip, keep user pip, which by default isn’t accessible via the
pip
command (so you need to usepython -m pip
).You could add
~/.local/bin
to your PATH env var, to be able to use thepip
command with the user pip.Fix option 2:
Remove user pip, keep the system pip, which is older but by default has a working
pip
command in the path.Always prefer “python3 -m pip” over pip3" or even better “/usr/bin/env python3 -m pip” it is safer and allow to avoid this issue with pip10
@fake-name additionally the general suggestion for usage on all distros is - use a virtualenv, don’t vandalize the system, and that works - people just font follow it and then wonder when stuff breaks and blame pip
there has been plenty of manual and automated testing with virtualenvs which works
also in a virtualenv at least there should be no debian made pip3 command - so what the heck are you talking about wrt this breaking in virtualenvs - please provide enough data to actually verify instead of whining about breakages without providing the data needed to verify them
pip is volunteer driven, not a company with dozens of employees
@pradyunsg in many cases the solutions given in that comment won’t work. Under a fresh Ubuntu 17.10, run
pip install --upgrade pip
: after that thepip
command will be broken, and the solutions from the comment won’t fix it. And they shouldn’t!Having a system installed pip 9 with a user installed pip 10, makes the system pip script try to import main() from the user pip 10, with an incorrect import path. Hash -r or -d won’t fix that because the pip command will still run the system pip by default. And neither will fix it upgrading user pip, as system pip will still be 9, user pip will still be 10, so the import will keep failing.
The solution for those cases must be uninstalling one of both pips.
python -m pip uninstall pip --user
, keeping the system pip, which is olderor
sudo apt remove python-pip
and keep the user installed pip, which won’t be accessible by runningpip
in the terminal by default. You will either need to run it withpython -m pip
, or add the paths to your PATH env var, etc.All of this applies to both, pip under python 2 and 3.
Same issue on Fedora, too.
thats a debian issue
I found an unrelated cause of this pip v10 issue. When upgrading pip using a very old system pip (v1.5.6 on Debian Jessie, i.e. oldstable) for which --system is the default, I noticed that incorrect scripts were installed, e.g.
/usr/local/bin/pip
containingfrom pip import main
– which I found by looking at the files. I assume this is because the older pip (or perhaps installation packages that it uses) install the .whl file incorrectly.python -m pip install --force-reinstall pip
fixed this.Hey @rod-app!
We’ve noticed this and worked with OS vendors to avoid this in future versions of pip. – #5346.
@fisadev Sure, that’s needed to support user installable ‘pip install --user’ package. But I think it should be told to users "if you want to force the update to pip 10 before debian/ubuntu package is updated, you need to use
pip install --user --upgrade pip
and ensure$HOME/.local/bin
is in your path. It’s simple to do.I found pip3, version 9.0.1 installed in a virtualenv from a project and copied it to my /usr/bin and it works again. Here are the contents of the pip3 executable for those who would like to fix it themselves as well.
I’m sure all you have to do is save that to a file called pip3, make it executable by running sudo chmod +x ./pip3, run sudo apt remove python3-pip, and then copy it to the bin directory by running sudo cp ./pip3 /usr/bin. Here’s the raw file for those who just want to download and move it. pip3.zip
@fake-name There were two pre-releases made:
10.0.0b1
was released on Mar 31st10.0.0b2
was released on April 2nd