Python pip3 错误 - '_NamespacePath' 对象没有属性 'sort'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47955397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pip3 error - '_NamespacePath' object has no attribute 'sort'
提问by amadispstac
I tried to install a package through pip3, and I got this error. Every pip/pip3 command that I run gives me this error-
我尝试通过 pip3 安装软件包,但出现此错误。我运行的每个 pip/pip3 命令都会给我这个错误-
alexg@hitbox:~$ pip3 -V
Traceback (most recent call last):
File "/usr/local/bin/pip3", line 7, in <module>
from pip import main
File "/home/alexg/.local/lib/python3.5/site-packages/pip/__init__.py", line 26, in <module>
from pip.utils import get_installed_distributions, get_prog
File "/home/alexg/.local/lib/python3.5/site-packages/pip/utils/__init__.py", line 27, in <module>
from pip._vendor import pkg_resources
File "/home/alexg/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in <module>
@_call_aside
File "/home/alexg/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
f(*args, **kwargs)
File "/home/alexg/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_working_set
dist.activate(replace=False)
File "/home/alexg/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
declare_namespace(pkg)
File "/home/alexg/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
_handle_ns(packageName, path_item)
File "/home/alexg/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2092, in _handle_ns
_rebuild_mod_path(path, packageName, module)
File "/home/alexg/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2121, in _rebuild_mod_path
orig_path.sort(key=position_in_sys_path)
AttributeError: '_NamespacePath' object has no attribute 'sort'
I have tried running pip3 install --upgrade pip setuptools
according to some answers, but every pip command gives me the error. I'm stuck and can't do anything.
我尝试pip3 install --upgrade pip setuptools
根据一些答案运行,但每个 pip 命令都给我错误。我被卡住了,什么也做不了。
What could have caused it, and how can this be fixed?
可能是什么原因造成的,如何解决?
I'm on Ubuntu 16.04
with Python 3.5.2
我在16.04
使用 Python 的Ubuntu3.5.2
回答by jkjung13
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
我在 python 3.5.2 和 pip3 (9.0.1) 上遇到了同样的问题。我按照以下解决方法修复了它: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"
更具体地说,我编辑了这个文件的第 2121~2122 行:“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]
回答by Thai
Upgrading setuptools worked for me:
升级 setuptools 对我有用:
pip3 install --upgrade setuptools
回答by Foreever
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.
这听起来可能很奇怪,因为我遇到了这个问题并且我已经尝试了 SO 和 GitHub 问题中提到的所有内容。但是后来我用easyinstall安装了pip,pip命令正在运行。也许现在有 2 个 pip 包了。但是错误消失了。
easy_install pip
easy_install pip
回答by mtlynch
I'm using virtualenv
and upgrading setuptools didn't work for me. What did work was this workaround:
我正在使用virtualenv
和升级 setuptools 对我不起作用。什么工作是这个解决方法:
pip uninstall setuptools -y && pip install setuptools
回答by Dataman
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
.
另一个答案,但以下是最终为我解决问题的答案。由于pip
受到损害,我无法使用它来升级自身,或者setuptools
也使用easy_install
它带来了同样的问题。所以我尝试pip
使用Python
.
The answer:
答案:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip install --upgrade setuptools
回答by fraz
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
我尝试了上述大部分建议的解决方案,但没有任何效果,因为 pip3 完全不起作用,然后我发现了这个: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.
我想这可能是我们大多数人的问题。我尝试了建议的解决方案,并且 pip3 再次运行。
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:
对我来说,matplotlib 不存在于 dist-packages 中,而是存在于 site-packages 中,因此对我有用的命令是:
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.
当然,您需要根据您的 matplotlib 版本和位置更改上述路径。
回答by adrpino
This probably means that your dependencies got messed up.
这可能意味着您的依赖项搞砸了。
Try to uninstall pip3 from scratch and it should work.
尝试从头开始卸载 pip3,它应该可以工作。
In case it doesn't work, delete your pip
installation. In your case:
如果它不起作用,请删除您的pip
安装。在你的情况下:
rm -r /home/alexg/.local/lib/python3.5/site-packages/pip/
And to be sure next time, best to work with virtual environments:)
并且确保下次最好使用虚拟环境:)
回答by markroxor
try -pip install -U pip
pip install -U setuptools
尝试 -pip install -U pip
pip install -U setuptools
if editing __init__.py
doesnt help.
如果编辑__init__.py
没有帮助。
回答by Kazuya Gosho
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.
解决了这个问题。
回答by AnotherShruggingPhysicist
I had this same problem, and was unable to run any command with pip3 (including any commands like pip3 install --upgrade pip setuptools
).
我遇到了同样的问题,无法使用 pip3 运行任何命令(包括任何命令,如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.
我发现的唯一解决方法是完全卸载并重新安装 python 3 ( sudo apt-get remove python3
, sudo apt-get install python3
, sudo apt install python3-pip
),现在 pip3 再次正常工作。