Trying to uninstall TensorFlow or Matplotlib pip package throws AttributeError: '_NamespacePath' object has no attribute 'sort'

This can be triggered by a variety of operations but in my case I am dealing with Lambda Stack and TensorFlow and trying to uninstall a pip installed TensorFlow: sudo pip3 uninstall tensorflow-gpu.

ubuntu@lambda-quad:~$ sudo pip3 uninstall tensorflow-gpu                                                                                                                                                    
Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 7, in <module>
    from pip._internal import main
  File "/usr/local/lib/python3.5/dist-packages/pip/_internal/__init__.py", line 42, in <module>
    from pip._internal import cmdoptions
  File "/usr/local/lib/python3.5/dist-packages/pip/_internal/cmdoptions.py", line 16, in <module>
    from pip._internal.index import (
  File "/usr/local/lib/python3.5/dist-packages/pip/_internal/index.py", line 15, in <module>
    from pip._vendor import html5lib, requests, six
  File "/usr/local/lib/python3.5/dist-packages/pip/_vendor/requests/__init__.py", line 86, in <module>
    from pip._vendor.urllib3.contrib import pyopenssl
  File "/usr/local/lib/python3.5/dist-packages/pip/_vendor/urllib3/contrib/pyopenssl.py", line 47, in <module>
    from cryptography import x509
  File "/usr/lib/python3/dist-packages/cryptography/x509/__init__.py", line 7, in <module>
    from cryptography.x509.base import (
  File "/usr/lib/python3/dist-packages/cryptography/x509/base.py", line 14, in <module>
    from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa
  File "/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/asymmetric/rsa.py", line 14, in <module>
    from cryptography.hazmat.backends.interfaces import RSABackend
  File "/usr/lib/python3/dist-packages/cryptography/hazmat/backends/__init__.py", line 7, in <module>
    import pkg_resources
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2927, in <module>
    @_call_aside
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2913, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2952, in _initialize_master_working_set
    add_activation_listener(lambda dist: dist.activate())
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 956, in subscribe
    callback(dist)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2952, in <lambda>
    add_activation_listener(lambda dist: dist.activate())
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2515, in activate
    declare_namespace(pkg)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2097, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2047, in _handle_ns
    _rebuild_mod_path(path, packageName, module)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2066, in _rebuild_mod_path
    orig_path.sort(key=position_in_sys_path)
AttributeError: '_NamespacePath' object has no attribute 'sort'

This is caused by a namespace package file in matplotlib. This can be removed with:

sudo rm /usr/local/lib/python3.5/dist-packages/matplotlib-*-nspkg.pth

This will then fix the issue and you can simply remove what you were trying to remove before:

ubuntu@lambda-quad:~$ sudo pip3 uninstall tensorflow-gpu
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Uninstalling tensorflow-gpu-1.7.0:
  Would remove:
    /usr/local/lib/python3.5/dist-packages/external/*
    /usr/local/lib/python3.5/dist-packages/tensorflow/*
    /usr/local/lib/python3.5/dist-packages/tensorflow_gpu-1.7.0.dist-info/*
Proceed (y/n)? y
  Successfully uninstalled tensorflow-gpu-1.7.0
ubuntu@lambda-quad:~$ sudo pip3 uninstall matplotlib
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Uninstalling matplotlib-2.2.2:
  Would remove:
    /usr/local/lib/python3.5/dist-packages/matplotlib-2.2.2.dist-info/*
    /usr/local/lib/python3.5/dist-packages/matplotlib/*
    /usr/local/lib/python3.5/dist-packages/mpl_toolkits/axes_grid/*
    /usr/local/lib/python3.5/dist-packages/mpl_toolkits/axes_grid1/*
    /usr/local/lib/python3.5/dist-packages/mpl_toolkits/axisartist/*
    /usr/local/lib/python3.5/dist-packages/mpl_toolkits/mplot3d/*
    /usr/local/lib/python3.5/dist-packages/mpl_toolkits/tests/*
    /usr/local/lib/python3.5/dist-packages/pylab.py
Proceed (y/n)? y
  Successfully uninstalled matplotlib-2.2.2

Thanks to @steve for this fix.