pip: ImportError in system pip wrappers after an upgrade
Overview
After upgrading to pip 10 or higher, many users are encountering error like
ImportError: cannot import name 'main'
TypeError: 'module' object is not callable
ImportError: No module named _internal
ModuleNotFoundError: No module named 'pip._internal'
These are caused by an incorrect attempt to upgrade pip, which has typically resulted in (parts of) multiple versions of pip being installed in the same Python installation, and those parts being incompatible.
It should be noted that these issues are invariably not problems with pip itself, but are due to incorrect use of pip, or unexpected interactions with system scripts that are not controlled by pip. So while we’ll try to help you solve your issue, this is not the “fault” of pip, and you will have to be prepared to do at least some of the debugging and fixes on your own.
You can reach out to your Python provider (eg: Linux Distro, Cloud Provider, etc) for help with this issue.
General Advice
First, some general advice. It is assumed that anyone encountering issues will have failed to follow some part of this advice. Listing these items here is not intended to imply that “it’s your fault and we won’t help”, but rather to help users to identify what went wrong, so that they can work out what to do next more easily.
- Only ever use your system package manager to upgrade the system pip. The system installed pip is owned by the distribution, and if you don’t use distribution-supplied tools to manage it, you will hit problems. Yes, we know pip says “you should upgrade with pip install -U pip” - that’s true in a pip-managed installation, ideally distributions should patch this message to give appropriate instructions in the system pip, but they don’t. We’re working with them on this, but it’s not going to happen soon (remember, we’re looking at cases where people are upgrading old versions of pip here, so patches to new versions won’t help).
- Never use sudo with pip. This follows on from the first point. If you think you need to use sudo, you’re probably trying to modify a distribution-owned file. See point 1.
- Prefer to use
--user
. By doing this, you only ever install packages in your personal directories, and so you avoid interfering with the system copy of pip. But there arePATH
issues you need to be aware of here. We’ll cover these later. Put simply, it’s possible to follow this advice, and still hit problems, because you’re not actually running the wrapper you installed as--user
.
Debugging the Issue
Before trying to work out what’s going on, it’s critically important that you understand precisely what scripts you are running and what versions of pip the relevant Python interpreter is using.
First, identify the full absolute path of the executable script that you are running. That’s often in the traceback you get, but if it isn’t, you can use OS tools like which
, or Python’s shutil.which
to identify the right script. Watch out for your shell confusing the issue, with aliases or potentially stale hashed commands.
Once you have identified the script, make sure you can reproduce the problem using the absolute path to that script. If you can’t, you probably found the wrong script, so check again.
Second, work out which version of Python the script is using to run pip. You’ll often be able to get that from the shebang line of the script. This can often be the trickiest problem, as wrapper scripts can take many forms depending on what tool created them. In the worst case, you can simply make an intelligent guess at this point.
Once you know which Python is running pip, you can run python -m pip
to invoke pip. In most cases, this will work fine, as it’s the wrapper causing the issue, and not pip itself. Running pip via python -m
in this way is often a perfectly acceptable workaround for the issue (at least in the short term).
Now run python -m pip --version
. This will give you the exact version and location of the installation of pip that your Python is seeing.
At this point, you’re usually done - the fundamental cause of all these problems is running a wrapper script which is written expecting to see a version of pip older than pip 10 (that’s why it imports pip.main
) under a Python interpreter that sees a copy of pip that’s version 10 or later.
Fixing the Issue
The problem, of course, is fixing the issue. And that’s where you really are on your own. If you have changed your system installation, you really need to put it back the way that the distribution installed it. That may well require an uninstall and reinstall of your distribution’s pip package. Reinstalling is easy, of course - but uninstalling may require manually removing incorrect files. Or you may be able to force-reinstall with your distribution package manager, simply overwriting the incorrect files. Of course, this reverts you to the system-supplied version of pip. If you need a newer version, you should ask your distribution vendor, or use something like virtualenv to install it independently of your system packages.
It may be that you’re simply running the “wrong” wrapper script. Maybe you did a --user
install of a new version of pip, but your PATH is set to run the system version of the wrapper rather than the user-local one installed with pip. In that case, you can simply fix your PATH. That’s usually the issue for people who do pip install --user --upgrade pip
and get the pip.main
error.
As already noted, python -m pip
is a reliable workaround, at the cost of using a more verbose command to invoke pip.
Community Advice
The comments section of this issue is open for people to discuss specific problems, and to share potential solutions. Just to be clear, it’s quite likely with problems of this nature that you’ll need to modify system-supplied files or settings. You do so at your own risk. If you’re not comfortable modifying your OS, or running as root, you should seek expert advice. To put it another way, if by following suggestions given here, you break your system, you get to keep the pieces. There’s only so much that can be achieved remotely.
Also, the pip developers don’t provide any guarantees that advice in the comments on this issue is correct, or that it won’t damage your system. Again, use at your own risk.
Related Issues
The following issues have been reported which are related to this issue: #5240, #5221, #5588, #5495, #5493, #5487, #5447, #5432, #5373, #5326, #5318, #5253
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 388
- Comments: 122 (27 by maintainers)
Links to this issue
- 如何在同一文件中安装模块后导入模块? - 问答 - 腾讯云开发者社区-腾讯云
- python - "E: Unable to locate package python3-pip" - Stack Overflow
- python - pip: no module named _internal - Stack Overflow
- Solved: Setting the "Epic Status" using python and JIRA AP...
- python - pip no longer working after update error 'module' object is not callable - Stack Overflow
- linux - How to install and import Python modules at runtime - Stack Overflow
- Help! already wasted 2 day of my life on this ):
- Installation spotify_dl fails on macOS
- Unable to run ansible playbook from Rundeck
- WARNING: pip is being invoked by an old script wrapper
- Can't seem to install Python YAML support
- [Cross Post]Error when trying to install impacket on Kali
- Error when trying to install impacket on Kali
- WARNING: pip is being invoked by an old script wrapper.
- Could someone help me fix pip?
- running package from github
- Pandas on Mac Book
- Trying to launch a website, and runserver command will not work :'(
- 2.10 loading time
- pip install pyinptplus failing for automatetheboringstuff
Commits related to this issue
- make(pip): fix installs of pip to isolate to user The version of pip that is installed via apt should not be updated by running `pip install --upgrade pip` see: pypa/pip#5599 Workarond: when install... — committed to gurpreetatwal/dotfiles by gurpreetatwal 6 years ago
- fix documentation building environment see https://github.com/pypa/pip/issues/5599 — committed to nuest/reference-implementation by nuest 6 years ago
- make(pip): fix installs of pip to isolate to user The version of pip that is installed via apt should not be updated by running `pip install --upgrade pip` see: pypa/pip#5599 Workarond: when install... — committed to gurpreetatwal/dotfiles by gurpreetatwal 6 years ago
- Fix pip problem (https://github.com/pypa/pip/issues/5599) in Dockerfile — committed to minepy/mictools by davidealbanese 5 years ago
- Dockerfile: Change base image to use stretch jessie apt source urls have changed to archive.debian.org https://lists.debian.org/debian-devel-announce/2019/03/msg00006.html Bump the base image to use... — committed to balena-io/autohat by ZubairLK 5 years ago
- Update documentation in light of common pip3 issue Using `python3.6 -m pip ...` to install libraries as apposed to using `pip3 ...` https://github.com/pypa/pip/issues/5599 — committed to second-state/smart-contract-search-engine by tpmccallum 5 years ago
- Using pip default version instead of trying to upgrade https://github.com/pypa/pip/issues/5599 — committed to AlexGascon/diabetelegram by AlexGascon 5 years ago
- fix: Pip bug https://github.com/pypa/pip/issues/5599 — committed to dougch/s2n-tls by dougch 5 years ago
- Install latest pip There's a whole host of problems with pip, it's distribution and installation. The cleanest solution seems to be to just install it directly from pip.pypa.io instead apt package py... — committed to ska-telescope/lmc-base-classes by deleted user 4 years ago
- Install latest pip There's a whole host of problems with pip, it's distribution and installation. The cleanest solution seems to be to just install it directly from pip.pypa.io instead apt package py... — committed to ska-telescope/lmc-base-classes by deleted user 4 years ago
- Install latest pip There's a whole host of problems with pip, it's distribution and installation. The cleanest solution seems to be to just install it directly from pip.pypa.io instead apt package py... — committed to ska-telescope/lmc-base-classes by deleted user 4 years ago
- pip module usage recommendation from https://github.com/pypa/pip/issues/5599 — committed to stynoo/s3-disk-usage by deleted user 4 years ago
- Avoid calling pip script There are times where this is broken https://github.com/pypa/pip/issues/5599 and either fails to work or prints a warning — committed to jenshnielsen/Qcodes by jenshnielsen 4 years ago
- Avoid calling pip script There are times where this is broken https://github.com/pypa/pip/issues/5599 and either fails to work or prints a warning — committed to jenshnielsen/Qcodes by jenshnielsen 4 years ago
- Fix usage of pip3 according to https://github.com/pypa/pip/issues/5599 — committed to frgomes/bash-scripts by frgomes 4 years ago
- Fix usage of pip3 according to https://github.com/pypa/pip/issues/5599 — committed to frgomes/bash-scripts by frgomes 4 years ago
- Undo instructions Revert installation instruction upgrades; https://github.com/pypa/pip/issues/5599 Temporarily revert pylint/PEP8 badge due to SVG embedding in PyPi. — committed to StochSS/GillesPy2 by makdl 4 years ago
- Replace pip with python -m pip Update install instructions to avoid these warnings: ``` WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. Please se... — committed to alanyee/aws-cli by alanyee 4 years ago
- Install project and all dependencies to venv Use virtual environment within docker container due to: https://github.com/pypa/pip/issues/5599 — committed to cutwater/pulp-galaxy-deploy by cutwater 4 years ago
- Use new pip script wrapper WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. Please see https://github.com/pypa/pip/issues/5599 for advice on fixing ... — committed to bionicBUG/pyrobud by bionicBUG 4 years ago
I followed some instructions elsewhere and ran
and hit upon this issue.
fixed it, returning me to the system pip
I have to say, although I don’t fully understand the complications involved, it doesn’t feel like pip should self-immolate like this.
The advice above:
conflicts with the cheerful siren call:
This seems to be some kind of tarpit 😕
The bug happens when not using virtualenv, using virtualenv fixes the problem on my side. To fix the duplicate pip problem (described above) and downgrade to original pip version, I just
rm -rf ~/.local/lib/python2.7/site-packages/
(beware!).I would add this advice of not upgrading your system pip in the documentation. I just followed these instructions and killed my raspbian pip and pip3 installs.
On the page: https://packaging.python.org/tutorials/installing-packages/#ensure-pip-setuptools-and-wheel-are-up-to-date it lists
python -m pip install --upgrade pip setuptools wheel
, which is in conflict with recommentation 1 and 3 in this issue.I think I restored that with:
After that my raspbian PI zero
pip
andpip3
report the ancient 9.0.1 versions, which is the latests available according to https://tracker.debian.org/pkg/python-pip The request to update pip in debian repo can be found here: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=901393I wonder how
npm
andcpan
solve this problem? In any case, I just uninstalled systempip3
and created my own:I’m the only user running it on this system anyway. Victory!
The Ubuntu’s
python-pip-9.0.1
installed(via apt get python-pip) is conflicting with Latestpip-18.0
provided by pypa. Sosudo rm -rf ~/.local/lib/python2.7/site-packages/
(Beware!) removes the ubuntu’s python-pip and resolves the problem. Only one pip can exist on the system. Thanks @denisglotovAlso Upgrading pip doesn’t help as it does’nt overwrite the preexisting installed python-pip but instead create another copy of pip in different directory causing the conflict.
I am pretty new to coding, however I managed to fix it in a kind of odd way. My problem was that there was nothing in my
/usr/bin/
folder regarding pip. to fix this I had my buddy upload theget-pip.py
program to raspberry pi, from which I downloaded the code to my computer and ran it. It then over-rode the old pip program and installed the new one.Here’s the code we used: working computer to pi:
scp /whatever_your_directory_is/get-pip.py pi@pi's_address:/pi's_directory
pi to my computer (the last period is important):scp pi@10.200.130.65:/pi's_directoryi/get-pip.py .
to download pip:sudo python get-pip.py
Sorry if it’s weird, but that’s what got it working for me
… and to be 100% clear, this issue is intended as a thread to help users understand and debug the various complex interactions around user config, system, 3rd party and pip behaviour that result in this type of error. The intention is to help users be able to identify where the problem lies, precisely so that they can direct issue reports at the right people.
If as a result of the diagnosis, a user confirms that there’s a pip issue, then we’d welcome a new issue being raised, with a good description of the problem, and a means of reproducing it that we can replicate with just pip.
But I will say that my experience, and the evidence in this thread, is that very often the problem turns out not to be with pip, but with some other part of the “stack” - sorry if I come across as dismissive as a result, but please understand that I, and most of the other pip devs, really have seen these issues before. In the case of pycharm, I raised a similar issue with them about their not using supported means of installing packages, a long time (years) ago - to my knowledge it never got fixed, unfortunately. Hence my expectation in your case that it’s a pycharm problem, and the traceback supports this.
To fix the PATH run Command Prompt as administrator:
pip install --upgrade --force-reinstall --ignore-installed pip
that fixed the issue(For ubuntu users) Final solution! No action required! You may want to upgrade pip using
python3 -m pip install --user --upgrade pip
But error occurs! This because new pip locates in$HOME/.local/bin
but your $PATH doesn’t point it. But actually.profile
have a script to load/.local/bin
to path! So just logout and login again. Then problem fixed. By the way, it seems make sense to have pip packages in user level and load it via .profile because this also for user level.@ErichBSchulz We’re aware of that - see #5346.
Maybe I should have more explicitly said “Only ever use your system package manager to upgrade your system pip”. The more detailed comment in that bullet point makes it clearer, but I’ve edited the post to make it explicit.
sudo apt-get --reinstall install python3-setuptools python3-wheel python3-pip
sudo -H pip3 install --upgrade pip
Worked for me
oh, it might be the case, that I installed pip from PyPA and I completely forgot about it. Thank you for correcting me @McSinyx .
Again, you seem to be putting words in my mouth or misunderstanding me intentionally.
I’m pointing out issues for others. Your saying I’m saying something I’m not. Is English your first language? I’m happy to translate or find a way to make myself clearer.
There are multiple issues that get these errors.
I’m not blaming pip devs for pycharm’s/jetbrains broken code.
I am blaming JetBrains devs for not actively working as software craftspeople and actively working to mitigate these issues by doing things like add workarounds to unblock people, or even just better error messages. But If the error path results are known, why not add some debugging and diagnostics or at least a message that links to this thread?
Also, A lot of people who get these errors never themselves touch pip, and thus logically they can’t be at fault, so the dismissive tone is at best rude, and at worst targeting the wrong people and making them a victim of anger about a problem they are also innocent victims of.
Jetbrains Resharper uses a bad way to manage a virtualenv and last I checked uses its own packaged tools to do some of that management; This is why MacOS virtual environments are not compatible with numpty and other things by default, why you have these issues, because Pycharm uses its own package of a tool it calls venv instead of the virtual environment toolset that should be standard. The user is never told this and so the user rightly assumes that pip is at fault when in reality you can get this exact same error trying to install things that are not pip, without even touching pip.
Should Jetbrains be a more responsible member of the python community? Yes, Absolutely. But the python community could also do more to keep them in check, and can do more to make the common errors cited in this thread a mitigated problem that can more easily be worked around or could include some form of official guidance, and that’s not happening either.
@duaneking It’s a dev error but not from the pip devs. If devs of software A write it in a way relies on internals of library B then devs of software A are to blame. The leading underscore
_
should be sufficient indication for “use at your own risk”. So you indeed seem to be blaming wrong devs.Please demonstrate the pip bug with a clean install of pip not involving pycharm. I’m happy to address pip bugs, but we can’t control when other tools use pip in ways that are explicitly unsupported (in this case, calling pip’s internal functions).
@RafalSkolasinski It’s far safer, because you know you’re getting the version of pip installed with the version of Python that you’re running. Using the executable wrappers involves
PATH
issues to ensure that the wrapper you expect is getting run.pip
executable is being used rather than the one generated by the version of pip you want to run.Many errors that we’ve seen boil down to “you aren’t actually running what you think you are running” - either because people are inadvertently running the wrong wrapper, or because they are running a wrapper that isn’t created (and hence isn’t supported) by pip. Using
python -m pip
avoids these.Look at it this way - if
python -m pip
fixes the issue, then the problem isn’t with pip. So fixing the wrappers is going to need either changes to your environment (PATH
, most likely) or fixes from your distro vendor. Whether you want to work on a “proper” fix, given thatpython -m pip
is a workaround, is up to you.Personally, I think people would find it far easier to just use
python -m pip
. But changing years of muscle memory isn’t easy, so I doubt that will happen quickly…I ran into the same problem. It seems that there is a conflict between pip in system and pip in python site-packages for me in my ubuntu. After upgrading the pip as the pip command suggests, I removed the system pip using sudo apt remove python-pip And restarting the bash, the error goes away.
This worked for me after multiple tries on other methods.
what do you think I should do?
@duaneking
No. If pip is broken, it needs to be fixed. However if a user is using it wrong, there is nothing that needs to be done. You dont add workarounds to fix people using software incorrectly, you educate those users on how to properly use the software. In this case, that was attempted via an issue raised with jetbrains. The fact that the user (jetbrains) didn’t fix it is not pip’s problem.
#7499 has been done, to partially mitigate this problem for now. We will remove those additional wrappers at a later date, so please treat the warning that pip spews out about “old wrappers” as serious and solve the underlying issues (or at least help by identifying them and letting us know here).
@oteph There’s nothing to fix here. See the original issue description, specifically:
We can’t provide an “official” statement on how to upgrade scripts (system pip wrappers) that we didn’t provide and that we don’t maintain.
OK, cool. I think one of the problems here is that no-one knows what “best practice” is (and everyone’s quite reasonably focused on fixing their own problem, not working out a general solution), which is why we keep getting the same sort of questions. So having somewhere we can thrash out the “right” solution will be a good step forward.
It seems to me that the advice:
Only ever use your system package manager to upgrade the system pip.
isn’t really sufficient: commands likepip3 install pipenv
will cause pip to be upgraded, stopping it (or rather, the ubuntu 16.04 wrapper) from working.@pfmoore It’s from tensorflow, not PyCharm. See install.R; below are the two functions that look most relevant to me. And
tensorflow
shoots itself in the foot where it callspip_install
three times:This code finds that the native
pip 1.5.4
is older than 8.1, so it tries to upgrade 3 packages. Firstpip
, which works because thepip
script that tensorflow installed into my virtualenv, and which I reproduced above, is compatible with 1.5.4, and thus upgrades topip 19.0.3
.Next (still because pip 1.5 is older than 8.1) it tries to upgrade
wheel
, but now the same supplied script~/.virtualenvs/r-tensorflow/bin/pip
is incompatible with the just-installedpip 19.0.3 from ~/.virtualenvs/r-tensorflow/local/lib/python2.7/site-packages/pip
, which no longer providesmain
. So the second invocation of pip fails, and thus the whole installation oftensorflow
!My workaround was to do the following in a shell:
I fixed this by opening the files
/usr/local/bin/pip
and/usr/bin/pip
and replacing the linefrom pip import main
withfrom pip._internal import main
.@aktaseren Please use the code button.
@lemon-doge linux has it. Install homebrew for macs as an alternative.
Microsoft Windows [Version 6.1.7601] Copyright © 2009 Microsoft Corporation. All rights reserved.
C:\Users\Kiddy>pip install virtalenv WARNING: pip is being invoked by an old script wrapper. This will fail in a futu re version of pip. Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the unde rlying issue. To avoid this problem you can invoke Python with ‘-m pip’ instead of running pip directly. ERROR: Exception: Traceback (most recent call last): File “C:\Users\Kiddy\AppData\Roaming\Python\Python37\site-packages\pip_intern al\cli\base_command.py”, line 186, in _main status = self.run(options, args) File “C:\Users\Kiddy\AppData\Roaming\Python\Python37\site-packages\pip_intern al\commands\install.py”, line 258, in run isolated_mode=options.isolated_mode, File “C:\Users\Kiddy\AppData\Roaming\Python\Python37\site-packages\pip_intern al\commands\install.py”, line 604, in decide_user_install if site_packages_writable(root=root_path, isolated=isolated_mode): File “C:\Users\Kiddy\AppData\Roaming\Python\Python37\site-packages\pip_intern al\commands\install.py”, line 549, in site_packages_writable test_writable_dir(d) for d in set(get_lib_location_guesses(**kwargs)) File “C:\Users\Kiddy\AppData\Roaming\Python\Python37\site-packages\pip_intern al\commands\install.py”, line 549, in <genexpr> test_writable_dir(d) for d in set(get_lib_location_guesses(**kwargs)) File “C:\Users\Kiddy\AppData\Roaming\Python\Python37\site-packages\pip_intern al\utils\filesystem.py”, line 140, in test_writable_dir return _test_writable_dir_win(path) File “C:\Users\Kiddy\AppData\Roaming\Python\Python37\site-packages\pip_intern al\utils\filesystem.py”, line 153, in _test_writable_dir_win fd = os.open(file, os.O_RDWR | os.O_CREAT | os.O_EXCL) PermissionError: [Errno 13] Permission denied: ‘c:\program files\python37\Lib \site-packages\accesstest_deleteme_fishfingers_custard_dhjxxo’ WARNING: You are using pip version 20.0.2; however, version 20.1.1 is available.
You should consider upgrading via the ‘c:\program files\python37\python.exe -m p ip install --upgrade pip’ command.
C:\Users\Kiddy>
I think a better solution is for pip to remain update agnostic. If distros won’t keep pip up to date with their packages then we must force a newer version upon that distro when running
pip3 install -U pip
. I seriously doubt overwriting the pip installed with a distro with a newer version will break any python dependencies a distro is relying on. One of the great benefits of pip (along with npm and others) is that you can update your packages/dependencies agnostic of distro package updates. So you can actually keep your software updated without reliance on distro package maintainers. Instead, you get updates directly from PyPi from the python package maintainers.Current version of pip3 for Ubuntu 18.04 arm64 is too low(9.0.1), so I upgrade it with
pip3 install -U pip
and ran into this problem. If installed byget-pip.py
, then other problems will be caused, which is annoying.Just delete the old script wrapper in PATH after upgrade on my macos.
@ZoranPandovski it worked for me. When you run the script as administrator it will fix the PATH I updated the pip as a local user using
--user install
that is where problem started, so i how to reinstall it as administrator to fix the problem with PATH.--force-reinstall --ignore-installed
otherwise it will give you an error when you try to install.I had the same experience with this issue #5373. The only solution for me was uninstalling the pip.
sudo apt remove python3-pip
sudo apt purge python3-pip
And follow the installation guide on this site https://pip.pypa.io/en/stable/installing/ Now pip works like a charm.
So to use the latest version of pip, we have to install a local (per-user) version of pip, and leave the system-installed pip untouched. This is clear and helpful advice. Thanks. I am very lucky I saw this page in time, before using sudo to install.
But … what a mess. On a system with N users, there could be N separately-installed versions of pip, some of them identical. Spasmodically, all N users will decide they should have the latest and greatest version of pip, and upgrade their copies asynchronously. Creating this situation was a massive blunder. I’m sure it’s extremely difficult to fix now, but it’s really important!
This solution worked for me. Restarting bash was what I was missing before. Messy!
pip3 install --user pip
seems to break my system pip.Isn’t it a good idea to install the latest version of pip in the user’s home directory?