pip3 error - '_NamespacePath' object has no attribute 'sort'

社会主义新天地 提交于 2019-11-28 04:39:39

I met the same issue with python 3.5.2 and pip3 (9.0.1). And I fixed it by following this workaround: https://github.com/pypa/setuptools/issues/885#issuecomment-307696027

More specifically, I edited line #2121~2122 of this file: "sudo vim /usr/local/lib/python3.5/dist-packages/pip/_vendor/pkg_resources/__init__.py"

#orig_path.sort(key=position_in_sys_path)
#module.__path__[:] = [_normalize_cached(p) for p in orig_path]
orig_path_t = list(orig_path)
orig_path_t.sort(key=position_in_sys_path)
module.__path__[:] = [_normalize_cached(p) for p in orig_path_t]

Upgrading setuptools worked for me:

pip3 install --upgrade setuptools

This may sound weird, because I had this issue and I had tried everything mentioned in SO and GitHub issues. But then I installed pip with easyinstall and pip command is working. Maybe now there are 2 pip packages now. But error is gone.

easy_install pip

I'm using virtualenv and upgrading setuptools didn't work for me. What did work was this workaround:

pip uninstall setuptools -y && pip install setuptools

Yet another answer, but following is the one which eventually fixed the issue for me. Since pip was compromised I was unable to use it for upgrading itself or setuptools and also using easy_install was bringing up the same issue. So I tried to install pip using Python.

The answer:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py 
pip install --upgrade setuptools

This probably means that your dependencies got messed up.

Try to uninstall pip3 from scratch and it should work.

In case it doesn't work, delete your pip installation. In your case:

rm -r /home/alexg/.local/lib/python3.5/site-packages/pip/

And to be sure next time, best to work with virtual environments :)

I tried most of the above suggested solutions but nothing worked as pip3 was totally non-functional, then I found this: https://deeptalk.lambdalabs.com/t/trying-to-uninstall-tensorflow-or-matplotlib-pip-package-throws-attributeerror--namespacepath-object-has-no-attribute-sort/310

I think this is probably the issue with most of us. I tried the suggested solution and pip3 is working again.

For me the matplotlib was not present in dist-packages but instead it was in site-packages, so the command that worked for me was:

sudo rm ~/.local/lib/python3.5/site-packages/matplotlib-3.0.3-py3.5-nspkg.pth

ofcourse you need to change the above path based on your version and location of matplotlib.

try -
pip install -U pip
pip install -U setuptools

if editing __init__.py doesnt help.

Try : sudo easy_install pip and then

sudo easy_install setuptools

It probably happends because of messed-up dependencies.

I had the same problem using poetry.

Running

poetry run pip install --upgrade pip setuptools

instead of

pip install --upgrade pip setuptools

fixed the issue.

I had this same problem, and was unable to run any command with pip3 (including any commands like pip3 install --upgrade pip setuptools).

Only fix I've found was to completely uninstall and re-install python 3 (sudo apt-get remove python3, sudo apt-get install python3, sudo apt install python3-pip) and now pip3 is working properly again.

After trying all kinds of methods, such as reinstall pip, setuptools, and still can't fix the problem. I follow the instruction of https://github.com/pypa/pip/issues/4216#issuecomment-286348680 and only with an external warning. After that, I use pip to uninstall matplotlib. Finally, all things seem okay.

I had this problem consistently (and used the workaround below to use pip in the python3 interpreter). To my surprise, after uninstalling all versions of numpy and reinstalling the latest one, pip began working again. I can't say for sure what happened, but it might be something to try if everything else has failed.

The workaround (pieced together from several sources I don't recall) is

  1. start python3
  2. import pip twice (getting an error the first time)
  3. use pip.main within the interpreter

Here is a transcript:

>>> import pip
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py",
    __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 "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 13,
    from pip.exceptions import InstallationError, CommandError, Pi
  File "/usr/lib/python3/dist-packages/pip/exceptions.py", line 6,
    from pip._vendor.six import iteritems
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", l
    vendored("pkg_resources")
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", l
    __import__(modulename, globals(), locals(), level=0)
                                .
                               etc
                                .
AttributeError: '_NamespacePath' object has no attribute 'sort'
>>> import pip
>>> pkgs = ['asciiplotlib']
>>> pip.main(['install'] + pkgs + ['--upgrade'])
Collecting asciiplotlib
  Using cached https://files.pythonhosted.org/packages/15/c5/46a2d
Installing collected packages: asciiplotlib
Successfully installed asciiplotlib-0.1.8

I fixed this with follows:

$curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

For python2:

$python get-pip.py

For python3:

$python3 get-pip.py

Now it works for me.

None of the other posted answered worked for me, but running the follow command solved t:

sudo apt-get purge --auto-remove python3-pkg-resources python3-setuptools

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!