7

I think my pip is broken. I've tried everything from doing force reinstall to update everything but nothing seems to work.

when I do pip2 -v then I get the following:

Traceback (most recent call last):
  File "/usr/local/bin/pip2", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3144, in <module>
    @_call_aside
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3128, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3157, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 666, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 984, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 870, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==9.0.1' distribution was not found and is required by the application

when I do pip -v then I get the following:

Traceback (most recent call last):
  File "/bin/pip", line 7, in <module>
    from pip._internal import main
ImportError: No module named pip._internal

FYI: I'm on Mac OSX and am using Python 2.7.14

Please help!!

user10096621
  • 225
  • 2
  • 4
  • 16
  • 1
    Possible duplicate of [The 'pip==7.1.0' distribution was not found and is required by the application](https://stackoverflow.com/questions/39845636/the-pip-7-1-0-distribution-was-not-found-and-is-required-by-the-application) – Ishan Srivastava Jul 18 '18 at 01:06
  • @IshanSrivastava While that is the same problem, do any of the answers there explain the problem, or give a solution anyone should actually do? – abarnert Jul 18 '18 at 01:10
  • I'm pretty sure `/usr/local/lib/python2.7/site-packages` is the site-packages for Apple's pre-installed Python 2.7.10, not for whatever python.org/Homebrew/Anaconda/whatever 2.7.14 you installed. So you've apparently got a `pip 9.0.1` script that should go with your 2.7.14, but it thinks it goes with your 2.7.10, which either doesn't have `pip` at all, or has an older version. – abarnert Jul 18 '18 at 01:12
  • 2
    sudo easy_install pip==9.0.1 fixed it for me. – vks2106 Mar 13 '20 at 17:54

3 Answers3

13

Since you're on macOS, your computer already had a Python 2.7, pre-installed by Apple. If you're on macOS 10.13, it's 2.7.10; older versions of course have older versions.

Meanwhile, you've installed Python 2.7.14. You didn't tell us how—python.org installer, Anaconda, Homebrew, whatever—but that's fine.

The problem is that the Apple Python 2.7.10 is still your "primary" 2.7, so you somehow ended up with a pip 9.0.1 that installed its packages for your 2.7.14, but thinks it's supposed to run with the Apple 2.7.10 instead. That's why it's looking in /usr/local/lib/python2.7/site-packages, which is the site-packages for Apple's 2.7.10, not for your 2.7.14. And you either don't have pip for the Apple 2.7.10, or have an older version. Hence the error.


Headaches in dealing with multiple Python installations—especially multiple installations of the same version—are why the Python Packaging User Guide suggests that you:

  • Use python -m pip to run pip.
  • Use virtual environments if at all possible.

I don't know how you normally make sure you're running your 2.7.14 instead of Apple's 2.7.10, but whatever command you run, if you do the same thing with a -m pip, it's guaranteed to use your 2.7.14 rather than Apple's 2.7.10. For example, if you normally type python2, use python2 -m pip instead of pip2.

Meanwhile, if you activate a virtual environment, both python and pip (and other things like 2to3) are going to be the versions that go with that environment, no matter what else you happen to have installed and how confusing your overall system setup is.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Doing python --version gives me Python 2.7.14. When I do which python, I get: /usr/local/bin/python. And when I do python -m pip, I get: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named pip – user10096621 Jul 18 '18 at 01:24
  • @user10096621 OK, how did you install that 2.7.14? Installer package from python.org? Homebrew? Anaconda? `./configure && make && make install`? …? And did you change any of the default checkboxes/whatever while doing so? – abarnert Jul 18 '18 at 01:33
  • @user10096621 My guess is that you had a working `pip`, but then you accidentally installed `pip` for Apple's Python in a way that half-overwrote the other one, and now things are hopelessly messed up, and you're going to need to `rm /usr/local/bin/pip*`, then `python -m ensurepip --defaultpip`, then `python -m pip install --upgrade pip setuptools`. But please answer before trying that, because I don't want to give you advice that might make things worse instead of fixing them. – abarnert Jul 18 '18 at 01:35
  • I installed python using Homebrew. And I think you are right, because I've been trying a lot of solutions to fix my pip, which just kind of messed it more. Please let me know what can be done to fix now!! – user10096621 Jul 18 '18 at 17:25
  • @user10096621 If you can uninstall every Python besides the Apple one, wipe out your site-packages directories, reinstall the Homebrew Python, it’ll tell you if there are any conflicting scripts or symlinks left behind in /usr/local. Delete those and retry. Then you should have a working pip and a brand new clean install with no problems. If you can switch to using virtual environments (and also, do you actually need to be on 2.7 instead of 3.7?), it avoids even more possible problems in the future, but as long as you’re careful never to use the Apple Python that shouldn’t be necessary. – abarnert Jul 18 '18 at 17:29
  • @user10096621 One more thing: if you leave everything with the defaults, the Apple Python should require sudo to install anything, and the Homebrew one should not. So if you never use sudo, if you accidentally try to install something to the Apple Python, it should fail with a permissions error without changing anything, so no harm done. – abarnert Jul 18 '18 at 17:31
2

Just hit this same problem on my mac and I fixed it by

$ brew upgrade python@2

The above command, essentially, gives Apple's python 2.7.10 back to you.

$ which python
/usr/bin/python
$ python -V
Python 2.7.10

Pip worked immediately and can be upgraded to 19.0.1

pip --version
pip 18.1 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
pip install --upgrade pip setuptools
pip --version
pip 19.0.1 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)

And here is what brew upgrade python@2 has to say about python on mac

==> python@2
Pip and setuptools have been installed. To update them
  pip install --upgrade pip setuptools

You can install Python packages with
  pip install <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
Hang
  • 956
  • 6
  • 12
  • In case you still have problems check this other answer, needed both to fix my problem https://stackoverflow.com/questions/60298514/brew-reinstalling-python2 – vicco Jun 05 '20 at 20:02
0

As for me. I hava several python version in my OS. When i run pip2 -v, it shows

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 567, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 884, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 775, in resolve
    raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.VersionConflict: (pip 20.3.4 (/usr/local/lib/python3.5/dist-packages), Requirement.parse('pip==8.1.1'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/pip2", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 3238, in <module>
    @_call_aside
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 3222, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 3251, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 569, in _build_master
    return cls._build_from_requirements(__requires__)
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 582, in _build_from_requirements
    dists = ws.resolve(reqs, Environment())
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 770, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==8.1.1' distribution was not found and is required by the application

It shows that it find pip version in python3.5 directroy, so I think it maybe the multi version problem. So i execute ls -l /usr/bin/ |grep python, it shows that

lrwxrwxrwx 1 root   root          26 Aug 16  2019 dh_pypy -> ../share/dh-python/dh_pypy
-rwxr-xr-x 1 root   root        1056 Nov 24  2017 dh_python2
lrwxrwxrwx 1 root   root          29 Aug 16  2019 dh_python3 -> ../share/dh-python/dh_python3
lrwxrwxrwx 1 root   root          23 Mar  2  2021 pdb2.7 -> ../lib/python2.7/pdb.py
lrwxrwxrwx 1 root   root          23 Jan 27  2021 pdb3.5 -> ../lib/python3.5/pdb.py
lrwxrwxrwx 1 root   root          31 Oct 25  2021 py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx 1 root   root          26 Aug 16  2019 pybuild -> ../share/dh-python/pybuild
lrwxrwxrwx 1 root   root          24 Aug 11 07:16 python -> /etc/alternatives/python
lrwxrwxrwx 1 root   root           9 Nov 24  2017 python2 -> python2.7
-rwxr-xr-x 1 root   root     3492624 Mar  2  2021 python2.7
lrwxrwxrwx 1 root   root          33 Mar  2  2021 python2.7-config -> x86_64-linux-gnu-python2.7-config
lrwxrwxrwx 1 root   root          16 Nov 24  2017 python2-config -> python2.7-config
lrwxrwxrwx 1 root   root           9 Oct 25  2021 python3 -> python3.5
-rwxr-xr-x 2 root   root     4456208 Jan 27  2021 python3.5
lrwxrwxrwx 1 root   root          33 Jan 27  2021 python3.5-config -> x86_64-linux-gnu-python3.5-config
-rwxr-xr-x 2 root   root     4456208 Jan 27  2021 python3.5m
lrwxrwxrwx 1 root   root          34 Jan 27  2021 python3.5m-config -> x86_64-linux-gnu-python3.5m-config
lrwxrwxrwx 1 root   root          16 Mar 23  2016 python3-config -> python3.5-config
lrwxrwxrwx 1 root   root          10 Oct 25  2021 python3m -> python3.5m
lrwxrwxrwx 1 root   root          17 Mar 23  2016 python3m-config -> python3.5m-config
lrwxrwxrwx 1 root   root          16 Nov 24  2017 python-config -> python2.7-config
lrwxrwxrwx 1 root   root          29 Nov 24  2017 pyversions -> ../share/python/pyversions.py
-rwxr-xr-x 1 root   root        2909 Mar  2  2021 x86_64-linux-gnu-python2.7-config
lrwxrwxrwx 1 root   root          34 Jan 27  2021 x86_64-linux-gnu-python3.5-config -> x86_64-linux-gnu-python3.5m-config
-rwxr-xr-x 1 root   root        3185 Jan 27  2021 x86_64-linux-gnu-python3.5m-config
lrwxrwxrwx 1 root   root          33 Mar 23  2016 x86_64-linux-gnu-python3-config -> x86_64-linux-gnu-python3.5-config
lrwxrwxrwx 1 root   root          34 Mar 23  2016 x86_64-linux-gnu-python3m-config -> x86_64-linux-gnu-python3.5m-config
lrwxrwxrwx 1 root   root          33 Nov 24  2017 x86_64-linux-gnu-python-config -> x86_64-linux-gnu-python2.7-config

In my case, I want my pip2 work well, so I need make /usr/bin/python is python2. so I do sudo mv /usr/bin/python /usr/bin/python_bak and sudo cp /usr/bin/python2 /usr/bin/python Then pip2 works well. Don't forget mv python_bak python to recover. Sorry for my poor english.

Yatogami
  • 69
  • 7