Pip: `ModuleNotFoundError: No module named 'pip._vendor.pkg_resources'` AND `AttributeError: '_NamespacePath' object has no attribute 'sort'`

Created on 11 Mar 2017  路  24Comments  路  Source: pypa/pip

  • Pip version: I believe it is 8.1.2
  • Python version: 3.6.0
  • Operating system: Ubuntu Server 16.10 (x64)

Description:

Pip errors whenever I use the command. It doesn't matter which command. python3.6 -m pip errors.

What I've run:

logandark@LD-Server:~$ python3.6 -m pip
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 33, in vendored
    __import__(vendored_name, globals(), locals(), level=0)
ModuleNotFoundError: No module named 'pip._vendor.pkg_resources'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/lib/python3.6/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/lib/python3.6/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 21, in <module>
    from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 75, in <module>
    vendored("pkg_resources")
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 36, in vendored
    __import__(modulename, globals(), locals(), level=0)
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 646, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 616, in _load_backward_compatible
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2976, in <module>
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2962, in _call_aside
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 3004, in _initialize_master_working_set
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2535, in activate
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2109, in declare_namespace
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2058, in _handle_ns
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2078, in _rebuild_mod_path
AttributeError: '_NamespacePath' object has no attribute 'sort'
support

Most helpful comment

I had this issue on Arch and fixed it with:

sudo rm -rf /usr/lib/python3.6/site-packages/pip/
sudo pacman -Syu python-pip

All 24 comments

the first error is pretty much a linux disribution breaking pip intentionally as usual, nothing pip an do about it

the second error is related to not installing the ffi header and development packages, usually something like ffi-dev/ffi-devel or libffi-dev/libddi-devel, please figure the correct name for your distro

To me, it seems the first error is related to #4216.

@pradyunsg But this happens with _any_ Pip command.

@RonnyPfannschmidt How would I un-break pip? I need it.

If you're talking about Apport I've already disabled it and the exception still occurs. It was never related to Apport anyway. I updated the exception in the OP.

Sidenote: @LoganDark You have edited out the latter part of the OP. It would have been better to mention how you fixed the error or at the very least strike out the contents, instead of outright removing it.

But this happens with any Pip command.

Yes. That's the issue.

The fix is to upgrade to setuptools vendored with pip to >= 31.0.1 which fixes this issue. (editted)

@pradyunsg and how exactly would I do that? easy_install? as I said, pip is broken. I don't know how to upgrade python modules manually.

logandark@LD-Server:~$ python3.6
Traceback (most recent call last):
  File "/usr/lib/command-not-found", line 27, in <module>
    from CommandNotFound.util import crash_guard
ImportError: No module named 'CommandNotFound'

Oh wait, I had uninstalled python3.6. pip3.6 by itself works after upgrading python-setuptools, and CommandNotFound is still not found.

Update:

    ModuleNotFoundError: No module named 'setuptools'

@LoganDark This seems to be (somehow) related to your OS installation of pip, as @RonnyPfannschmidt said. You should raise this on your OS support channel. There's not much we can do here if the OS vendor has changed things.

Specifically:

  1. That CommandNotFound thing is something the OS has done, not Python (or pip) so we can't help diagnose that.
  2. python-setuptools is an OS package, you'd need to discuss that with Ubuntu support.

If you're hitting #4216, then there's no released fix yet in pip, so you'd need to revert to a version previous to ones with the issue. To get help with downgrading pip, you'd once again need to speak to your OS support channel. I believe it'd be some form of apt-get command you would need, but I don't personally know any more than that. I can't say precisely which version of pip you'd need to downgrade to, but I've asked that question on your behalf on #4216 - if you monitor that issue you should hopefully get the answer there. (You;d then still need to ask on Ubuntu support how to downgrade to that version, of course...)

4216 fix: sudo apt-get upgrade python-setuptools

and commandnotfound fix:
sudo dpkg --get-selections | grep -E "^python" | grep install | cut -f1 | xargs apt-get --reinstall -y install

Alright. Installing python3.6-dev made the problem (#4216) appear again. And uninstalling does not fix it.

So after using pip as root and seeing it working, I knew the erroneous package was in my ~/.local/lib/python3.6/site-packages. Here's what fixed it for me:

rm -rf ~/.local/lib/python3.6/site-packages/*

I'm not kidding either. I might also try reinstalling every package from a previous ls listing one by one to see which one caused the problem. I'm feeling lazy right now :p

zodb or one of its dependencies seems to be the cause of the issue. Simply having them installed breaks pip.

logandark@LD-Server:~$ pip3.6 install zodb
Collecting zodb
Collecting persistent>=4.2.0 (from zodb)
Collecting transaction>=2.0.3 (from zodb)
Collecting ZConfig (from zodb)
Collecting zodbpickle>=0.6.0 (from zodb)
Collecting zc.lockfile (from zodb)
Collecting zope.interface (from zodb)
Collecting six (from zodb)
  Using cached six-1.10.0-py2.py3-none-any.whl
Collecting BTrees>=4.2.0 (from zodb)
Collecting setuptools (from zodbpickle>=0.6.0->zodb)
  Using cached setuptools-34.3.2-py2.py3-none-any.whl
Collecting appdirs>=1.4.0 (from setuptools->zodbpickle>=0.6.0->zodb)
  Using cached appdirs-1.4.3-py2.py3-none-any.whl
Collecting packaging>=16.8 (from setuptools->zodbpickle>=0.6.0->zodb)
  Using cached packaging-16.8-py2.py3-none-any.whl
Collecting pyparsing (from packaging>=16.8->setuptools->zodbpickle>=0.6.0->zodb)
  Using cached pyparsing-2.2.0-py2.py3-none-any.whl
Installing collected packages: appdirs, six, pyparsing, packaging, setuptools, zope.interface, persistent, transaction, ZConfig, zodbpickle, zc.lockfile, BTrees, zodb
Successfully installed BTrees-4.4.1 ZConfig-3.1.0 appdirs-1.4.3 packaging-16.8 persistent-4.2.3 pyparsing-2.2.0 setuptools-34.3.2 six-1.10.0 transaction-2.1.2 zc.lockfile-1.2.1 zodb-5.2.0 zodbpickle-0.6.0 zope.interface-4.3.3
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
logandark@LD-Server:~$ pip3.6
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 33, in vendored
    __import__(vendored_name, globals(), locals(), level=0)
ModuleNotFoundError: No module named 'pip._vendor.pkg_resources'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/logandark/.local/bin/pip3.6", line 7, in <module>
    from pip import main
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 21, in <module>
    from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 75, in <module>
    vendored("pkg_resources")
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 36, in vendored
    __import__(modulename, globals(), locals(), level=0)
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 646, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 616, in _load_backward_compatible
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2976, in <module>
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2962, in _call_aside
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 3004, in _initialize_master_working_set
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2535, in activate
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2109, in declare_namespace
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2058, in _handle_ns
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2078, in _rebuild_mod_path
AttributeError: '_NamespacePath' object has no attribute 'sort'

Ignore the version warning. That of course is because I purged my local site-packages before doing this.

Closing as duplicate of #4216.
It should be fixed in master (pip install -I https://github.com/pypa/pip/archive/master.zip#egg=pip to test).

One suggested solution on Anaconda3 in windows 10:

  1. Find pip folder under "../Anaconda3/Lib/site-packages/", delete it manually.
  2. then in Anacoda prompt do : conda uninstall pip
  3. then install pip using your familiar way. E.g: conda install -c anaconda pip

Can anyone help here.
I am trying to install keras in conda using pip and I get below error:

(C:UsersHARSHALLAnaconda3) C:UsersHARSHALL>pip install --upgrade keras
Traceback (most recent call last):
File "C:UsersHARSHALLAnaconda3Scriptspip-script.py", line 6, in
from pip import main
File "C:UsersHARSHALLAnaconda3libsite-packagespip__init__.py", line 28, in
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "C:UsersHARSHALLAnaconda3libsite-packagespipvcsmercurial.py", line 9, in
from pip.download import path_to_url
File "C:UsersHARSHALLAnaconda3libsite-packagespipdownload.py", line 41, in
from pip._vendor.requests.adapters import BaseAdapter, HTTPAdapter
ImportError: No module named 'pip._vendor.requests.adapters'

@hlamba28 You either have a broken pip, or another module named pip.

@hlamba28 On my box I fixed a similar issue following steps suggested by @shuai-uulm
Please note that you'd need to run it in elevated Anaconda prompt and in the step 2 you might need to add --force option (conda uninstall pip --force)

I had this issue on Arch and fixed it with:

sudo rm -rf /usr/lib/python3.6/site-packages/pip/
sudo pacman -Syu python-pip

@itay-grudev .. That's it!!!

@itay-grudev that worked.
pacman updates broke pip for me.

@itay-grudev sometimes sudo pacman -Syu python-pip --force

@itay-grudev That worked. Thanks

@itay-grudev great bro thanks!!!!!!!

Was this page helpful?
0 / 5 - 0 ratings