pip3 缺少依赖项

pip3 缺少依赖项

我今天安装了最新的 Ubuntu Gnome,并正在设置我的机器以进行与数据科学相关的工作。当我尝试安装 tensorflow-gpu 时,遇到了 pip3 崩溃:

me@pc:~/Desktop$ 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'

我尝试清除 python3-pip 并重新安装,但仍然不起作用。我只需pip3在终端中运行并按 ENTER 即可获得上述结果。

有任何想法吗?

编辑:pip 和 pip3 的两条路径也不同:

me@pc:~$ which pip
/home/me/.local/bin/pip
me@pc:~$ which pip3
/usr/bin/pip3

答案1

我通过创建新的虚拟环境解决了这个问题

  1. 可以通过以下方式创建新的虚拟环境:

    python3 -m venv /tmp/newvenv
    
  2. 然后我检查了 pip 在新的虚拟环境中是否可以正常工作:

    cd /tmp/newvenv/bin
    ./pip3
    

这给了我如下的正常消息:

用法:
pip [选项]

命令:

安装 安装包。

下载 下载包。

...

然而,pip3 仍然失败。

    pip3

这仍然给我错误信息。

然后我决定将 pip 的工作版本复制到主目录。对于以下两个步骤(#3 和 #4),您可能需要保留备份。

  1. 我已将 pip 和 pip-9.0.1.dist-info 从虚拟环境覆盖到主目录:

    cp -r /tmp/newvenv/lib/python3.5/site-packages/pip ~/.local/lib/python3.5/site-packages/
    cp -r /tmp/newvenv/lib/python3.5/site-packages/pip-9.0.1.dist-info ~/.local/lib/python3.5/site-packages/
    
  2. 我还复制了以下两个文件:

    cp /tmp/newvenv/lib/python3.5/site-packages/pkg_resources/__init__.py ~/.local/lib/python3.5/site-packages/pkg_resources/ 
    cp /tmp/newvenv/lib/python3.5/site-packages/pkg_resources/py31compat.py ~/.local/lib/python3.5/site-packages/pkg_resources/
    

经过这些步骤后,pip3 在我的例子中可以正常工作。

答案2

您的 python setuptools 包已损坏。

先跑吧sudo apt install python3-setuptools

然后,

pip3 install --upgrade setuptools
pip3 install --upgrade pip

相关内容