pip: After pip 10 upgrade on pyenv "ImportError: cannot import name 'main'"
Maintainer note: Anyone that still gets this issue please see #5599.
- Pip version: 10.0
- Python version: 3.6.2
- Operating system: Ubuntu 16.04
Description:
After upgrading pip from 9.03 to 10.0 via pip install pip --user --upgrade in a pyenv environment pip refueses to start and raise this instead:
Traceback (most recent call last):
File "/home/kleinernull/.pyenv/versions/3.6.2/bin/pip", line 7, in <module>
from pip import main
ImportError: cannot import name 'main'
Traceback (most recent call last):
File "/home/kleinernull/.pyenv/versions/3.6.2/bin/pip", line 7, in <module>
from pip import main
ImportError: cannot import name 'main'
The content of all three different pip files is the same:
~ ⟩ cat .pyenv/versions/3.6.2/bin/pip ~
#!/home/kleinernull/.pyenv/versions/3.6.2/bin/python3.6
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal import main as _main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(_main())
~ ⟩ cat .pyenv/versions/3.6.2/bin/pip3 ~
#!/home/kleinernull/.pyenv/versions/3.6.2/bin/python3.6
# -*- coding: utf-8 -*-
import re
import sys
from pip import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
~ ⟩ cat .pyenv/versions/3.6.2/bin/pip3.6 ~
#!/home/kleinernull/.pyenv/versions/3.6.2/bin/python3.6
# -*- coding: utf-8 -*-
import re
import sys
from pip import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
The same happend with my 3.6.1 environment too.
Temporaly fix
According to the code of the master branch the import should be that:
#!/home/kleinernull/.pyenv/versions/3.6.2/bin/python3.6
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal import main as _main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(_main())
And this resolves the issue. I have no clue if this has something to do with the upgrade itself or with pyenv as environment.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 31
- Comments: 68 (20 by maintainers)
Links to this issue
- python - Error after upgrading pip: cannot import name 'main' - Stack Overflow
- python - ошибка при установке Django - Stack Overflow на русском
- python - ImportError: cannot import name main when running pip --version command in windows7 32 bit - Stack Overflow
- python - how to downgrade pip version 10.0.0 to pip version 9.0.1? - 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
- python - AttributeError: Module Pip has no attribute 'main' - Stack Overflow
Commits related to this issue
- Freeze version of pip due errors https://github.com/pypa/pip/issues/5240#issuecomment-381465991 — committed to SwissTierrasColombia/Asistente-LADM-COL by juusechec 6 years ago
- peg pip as suggested in https://github.com/pypa/pip/issues/5240#issuecomment-381673100 — committed to RussTedrake/underactuated by RussTedrake 6 years ago
- peg pip as suggested in https://github.com/pypa/pip/issues/5240#issuecomment-381673100 — committed to RussTedrake/underactuated by RussTedrake 6 years ago
- [ubuntu-dev] fix build by downgrading to pip 9.0.3 This works around "ImportError: cannot import name 'main'". See: https://github.com/pypa/pip/issues/5240#issuecomment-381673100 — committed to JanitorTechnology/dockerfiles by jankeromnes 6 years ago
- Work around broken pip 10 upgrade In recent CI runs we get: Installing collected packages: pip Found existing installation: pip 8.1.1 Not uninstalling pip at /usr/lib/python3/dist-packages, ou... — committed to optimsoc/optimsoc by imphil 6 years ago
- Pin to pip 9.0.3 See https://github.com/pypa/pip/issues/5240 — committed to quamotion/docker by qmfrederik 6 years ago
- ubuntu: Use pip from packages Upgrading pip by itself to version 10.0 results in errors.[0] [0] https://github.com/pypa/pip/issues/5240 Signed-off-by: Michal Rostecki <mrostecki@suse.com> — committed to vadorovsky/packer-ci-build by vadorovsky 6 years ago
- workaround for pip/python bug. see https://github.com/pypa/pip/issues/5240#issuecomment-382125488 — committed to mattharley/laradock by mattharley 6 years ago
- backup for pip 9.0.3 until issue resolves https://github.com/pypa/pip/issues/5240 — committed to yandexdataschool/Practical_RL by justheuristic 6 years ago
- downgrade pip to 9.0.1-2 https://github.com/pypa/pip/issues/5240 — committed to MichaelPak/csgo-server-python by deleted user 6 years ago
- appveyor: fork cibuildwheel to fix issue with pip self-upgrade https://github.com/pypa/pip/issues/5240#issuecomment-382989420 — committed to harfbuzz/uharfbuzz by anthrotype 6 years ago
- windows: upgrade pip with 'python -m pip' this fixes an "ImportError: cannot import name main" when self-upgrading pip to latest version 10 see https://github.com/pypa/pip/issues/5240#issuecomment-3... — committed to anthrotype/cibuildwheel by anthrotype 6 years ago
- appveyor: fix issue self-upgradig pip to v10 Installing with --user will leave the old pip.exe script in the /home/clupo/.pyenv/plugins/pyenv-virtualenv/shims:/home/clupo/.pyenv/shims:/home/clupo/.py... — committed to anthrotype/brotli by anthrotype 6 years ago
- appveyor: fix issue self-upgradig pip to v10 Installing with --user will leave the old pip.exe script in the $PATH, but running this will fail because pip 10 moved 'main' to internal modules. https:... — committed to anthrotype/brotli by anthrotype 6 years ago
- appveyor fix: use 'python -m pip' to upgrade pip (pypa/pip#5240) — committed to webrecorder/warcio by ikreymer 6 years ago
- appveyor fix: use 'python -m pip' to upgrade pip (pypa/pip#5240) — committed to webrecorder/pywb by ikreymer 6 years ago
- appveyor fix: use 'python -m pip' to upgrade pip (pypa/pip#5240) — committed to Rhizome-Conifer/conifer by ikreymer 6 years ago
- appveyor: fix issue self-upgradig pip to v10 (#663) Installing with --user will leave the old pip.exe script in the $PATH, but running this will fail because pip 10 moved 'main' to internal modules... — committed to google/brotli by anthrotype 6 years ago
- Pin pip version for now See https://github.com/pypa/pip/issues/5240 — committed to BenJuan26/OpenSkyStacker by BenJuan26 6 years ago
- appveyor: Upgrade pip with 'python -m pip' This reportedly works around https://github.com/pypa/pip/issues/5240 — committed to bdarnell/tornado by bdarnell 6 years ago
Fix for us was pinning to pip 9.03, so:
instead of
obvious fix but in case it helps somebody else!
I upgraded with pyenv, both python2 and python3, now pip2 not work and pip3 works After comparing pip2 and pip3, the difference is the import line
pip2
from pip import mainpip3
from pip._internal import mainAfter substitute pip2 import line with pip3 version, it works
Simply restarting my terminal fixed it for me.
Watch out for bash caching the executable location:
While I appreciate that the underlying issue reported here and in https://github.com/pypa/pip/issues/5221 is the environment, the cause is primarily that the import
from pip import mainwas broken as the packagepip.mainwas moved topip._internal.main. It would be trivial to add a link frompip.maintopip._internal.mainto fix this (whereas fixing the environment is a lot of work across many locations for many people). Is there a good reason to not do this?@davidjlloyd Because
from pip import mainwas never supported, basically. And while it’s easy to say “yes, but it’s a simple API and it just worked”, it really didn’t - we’ve had multiple bug reports from people expecting certain behaviours from it, that we simply don’t cater for (runningpip.mainin multiple threads, expectingpip.mainto not change the configuration of the logging system, …)Rather than keep explaining to people that they shouldn’t do this, and continually dealing with the fact that people are assuming “if I can find a function to call, it’s supported” we moved everything to the
_internalnamespace to make it abundantly clear that you’re not supposed to call it.The bulk of the complaints have come from people using
pip.main- which is ironic, as it’s so easy to call pip in the supported command line way viasubprocess. So of all the possible breakages, this is the easiest to fix - and yet people still haven’t fixed it, even after months of warnings. (Although to be fair to the Linux distributions, they don’t support people upgrading their system packages using pip, so reports like #5221 of cases where distro-supplied scripts break is fundamentally user error, not failure of the distros to address the pip 10 changes - something I’m sure they are handling perfectly well).Temporary Fix upgrade pip using.
Instead of
pip install -U pipFor pip2
pip2 install --upgrade pipWorked for me. I hope that helps someone.
I’m not sure that aggressively breaking existing uses of an unsupported feature instead of deprecating the feature for a couple of major releases is the best approach. Our initial reaction was to pin pip back to 9.0.3, and more lazy developers would probably just call it a day at that point. This would leave a lot of users stubbornly clinging to old releases, which I doubt anyone wants. However your motivation makes sense, and the eventual result is the same.
For any users hitting this issue, I think the nicest solution for replacing system or pyenv installations was kindly provided by @standag here: https://github.com/pypa/pip/issues/5221#issuecomment-381568428
This solution should work for anyone building in Docker such as @peteflorence without pinning to stale releases or anything horrible like that.
We are encountering the same trouble.
I just unrolled my pip installation.
python -m pip install --user --upgrade pip==9.0.3I’ve created a new Raspbian install on a Raspberry Pi 3B+. Nothing special - pretty vanilla configuration - and so far, everything has gone fine.
I’ve just reached the very last step in my install process:
…and now pip is completely borked:
As per the comment above, I’ve confirmed that ~i/.local/bin/pip runs fine. The error occurs with /usr/bin/pip. Running hash -d pip did not update the cached location, though.
Force-downgrading to 9.0.1 (via options line -Iv and --force-reinstall) also did not resolve the issue. Apparently, the only solution, besides not upgrading pip, is to run pip from ~/.local/bin/pip.
@p00j4 Importing pip from within your program is not supported - see the docs
All those issues seem to stem from the same problem: upgrading pip, but keeping on using an old launcher that still use the old entrypoint. A good way to avoid this kind of issue is to use
python -m pip.Even after following everything I am not able to downgrade pip. Keep getting error. Here’s how i fixed this:
$ sudo apt-get remove python-pip $ pip -v bash: /usr/bin/pip: No such file or directory
but when i try this $ python -m pip -V pip 10.0.0 from /home/user/.local/lib/python2.7/site-packages/pip (python 2.7)
so i removed complete folder of pip $ sudo rm -r /home/user/.local/lib/python2.7/site-packages/pip*
then again i try this $ python -m pip -V /usr/bin/pip: No module named pip
then install pip again $ sudo apt install python-pip $ python -m pip -V pip 8.1.1 from usr/lib/python2.7/dist-packages (python 2.7)
I installed a fresh Ubuntu distro today and tried to get some basic Python packages up and running. I was prompted to upgrade pip, and so I ran the command it told me to. This upgraded pip to version 10, which is apparently broken with the same error at the top of this thread. I haven’t done anything other than what a normal user would do to upgrade pip and install their favourite packages. Not even using pyenv.
None of the solutions here fix my problem. If I run
python -m pip install --upgrade pipI get “Requirement already up-to-date”. If I try to downgrade to version 9 I get the same initial error:After re-reading this thread, @sfsdfd’s solution to use
~/.local/bin/pipis what finally worked:Apparently this is working by (successfully) reverting back to my older version of pip. Adding this to
~/.bashrcis a nicer fix for me:It’s been answered repeatedly, Maybe not on this specific issue, but a search of the issue tracker should find plenty of discussion on the matter.
Because it was never supported. Why would we warn that we’re desupporting something we never supported? People assumed it was OK to read pip’s source code and use functions they found in there in their own code. It never was. We said it could break, and in pip 10 it did.
I’m not sure it’s as easy as you think. And I say that from the perspective of someone who has had to deal with issues where warnings added in pip 10 were being triggered when we hadn’t expected them to be…
And again, there’s no need to deprecate something that’s not supported in the first place.
What sort of thing? Breakage caused by us changing things that we don’t guarantee backward compatibility for? No. There’s no need for us to avoid that. Although in fact, we do try to manage the process, as a courtesy to our users (not an obligation!). In this case, we publicised the change 6 months in advance, offered suggestions for people who needed to change their code, and have been spending a lot of time since the release helping users who have had problems because software they rely on hasn’t heeded those warnings. That’s a lot of work that a very small volunteer group put into trying to mitigate a situation caused by people expecting support that was never offered or promised. You’re welcome.
We do have processes (deprecation warnings, etc) in place for changing things that we do guarantee compatibility for - but importing pip into your own program isn’t one of them.
I’m guessing you didn’t read the rest of his post. He was referring to using easy_install to install pip, not instead of pip:
Also he is not relying on pip’s internal implementation details. He’s using a fresh spinup of AppVeyor and upgrading pip using the command he specified, and nothing more:
Many other people in this thread are having the same problem. You can reproduce this with a basic installation of pip without doing anything special or dependent on pip’s internals. This has been documented by many people in the posts above.
I wanted to report that this issue also happens on Windows build when using AppVeyor for CI: The result is the following:
I have:
pip install --disable-pip-version-check --user --upgrade pipin severalappveyor.ymlscripts, based on the recommendation from this sample: https://github.com/ogrisel/python-appveyor-demo/blob/master/appveyor.yml#L111This has worked well until pip 10.x, and I suspect if others have created
appveyor.ymlbased on this will also run into this issue.Switching to
easy_install -U pipworked, but I do have several repos that worked before that now need to be updated to work with pip 10.x.Seems like the recommended approach for CI setup should be sticking with 9.0.3 or using easy_install.
I’ve fixed the issue by update the code in
/usr/bin/pip, changefrom pip import maintofrom pip._internal import main.Here details:
After
pip install --upgrade pip:After updated the
/usr/bin/pip:My system information:
Just to mention that
pip install --user --upgrade pipon Ubuntu 16.04 also breaks.Same here … Exact same output as @HayaoSuzuki and we don’t use pyenv
Yep, I think that seems to be the root of the issue, and happens across all different platforms mentioned in this thread, including Windows.
In case this helps anyone else, I can confirm that switching
appveyor.ymlpip upgrade from:pip install --disable-pip-version-check --user --upgrade pipto:
python -m pip install --upgrade pipdoes fix the issue. Now to update several more repos!
@arvoelke:
Installing pip using
easy_installcould well have issues, and if it does, we won’t be able to support you, because (as @pradyunsg said)easy_installis old, not actively maintained, and missing recent features. But if it works for you, and you don’t need us to help, then fine - no-one is stopping you.Define “the same”. If you mean "seeing errors from code that imports
pip.mainthen yes, but that’s not a problem with pip, it’s a deliberate, backward incompatible, change to the internal implementation of pip. If you want us to just say “tough, you shouldn’t be using the internal APIs of pip”, then fine - a single issue is enough, and we’ll just close it as “not a bug”. But if you want us to help you to work out what part of your environment has failed to follow the advice published 6 months ago, and find a workaround for you that lets you carry on using pip while your environment gets fixed by the vendor, then you’ll have to help us to help you. And saying “I have the same problem” on a report about a purely Unix utility, when you’re running on Windows, is definitely not helping us to help you…AFTER WASTING 3 HOURS THIS IS WHAT I GOT
TO UNINSTALL pip 10.0.0: sudo apt-get remove python-pip
but then u wont be able to install the correct required version of pip by the direct methods.
TO DOWNGRADE TO A KNOWN WORKING VERSION AFTER INSTALLING when pip10 throws the error: ImportError: cannot import name main you will have to run: python -m pip install pip==KNOW_WORKING_VERSION>
TO INSTALL ANY PACKAGE USING PIP10: sudo python -m pip install PACKAGE_NAME this worked.
Cheers 😃
pip 10.0, I also face
while trying to use
pip.main(['install', '-r', 'requirements.txt'])try to use pip2 install xx @HayaoSuzuki