Ubuntu – Unable to correctly install pip3 using apt-get

aptpippythonpython3

I am using Ubuntu 16.04, and was experiencing issues with my pip3. Specifically, when I attempted to uninstall a package via

sudo pip3 uninstall

I encountered

Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'

I attempted to resolve this issue by reinstalling pip3, specifically

 sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall

which succeeded. However, I no longer have a local version of pip3 at all

bash: /home/kurt/.local/bin/pip3: No such file or directory

Using which pip3 in a new terminal I find,

$ which pip3
/usr/bin/pip3

and attempting to use pip3 at all brings me

$ pip3
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)
ImportError: No module named 'pip._vendor.pkg_resources'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
    from pip import main
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 13, in <module>
    from pip.exceptions import InstallationError, CommandError, PipError
File "/usr/lib/python3/dist-packages/pip/exceptions.py", line 6, in <module>
    from pip._vendor.six import iteritems
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 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2927, in <module>
File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2913, in _call_aside
File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2952, 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 956, in subscribe
File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2952, in <lambda>
File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2515, in activate
File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2097, in declare_namespace
File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2047, in _handle_ns
File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 2066, in _rebuild_mod_path
AttributeError: '_NamespacePath' object has no attribute 'sort'

However, I am able to execute without incident when typing sudo -H pip3 -v. Reinstalling pip3 from apt, e.g.

sudo apt-get install python3-pip --reinstall

has no effect. If anyone has any insight, or has encountered a similar problem, I would greatly appreciate any input on how to approach this problem.

Best Answer

After uninstalling pip3 using

sudo apt-get remove python3-pip

I deleted the directory

$HOME/.local/lib/python3.5/site-pacakges

and reinstalled pip3 using apt-get

sudo apt-get install python3-pip

This appears to have resolved the issue. I no longer receive the aforementioned error when attempting to use pip3 or sudo pip3. Rather, pip3 -V, sudo pip3 -V, and sudo -H pip3 -V all identically and sucessfully return

pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)
Related Question