【问题标题】:pip3 -V should print pip3 version but returns No module named 'pip' in Ubuntu 16.04pip3 -V 应该打印 pip3 版本,但在 Ubuntu 16.04 中返回 No module named 'pip'
【发布时间】:2019-02-18 15:26:33
【问题描述】:

我在 Ubuntu 16.04 中使用 Python 3.7.2。我从默认的 Ubuntu 存储库安装了 python3-pip 包,apt 报告说我已经安装了 python3-pip 8.1.1-2ubuntu0.4(版本 8.1.1),但 pip3 确实不能正常工作。 pip3 -V 应该打印 pip3 版本,但它会返回 No module named 'pip'

root@VM-0-8-ubuntu:/usr/bin# python -V
Python 3.7.2
root@VM-0-8-ubuntu:/usr/bin# pip3 -V
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ModuleNotFoundError: No module named 'pip'
root@VM-0-8-ubuntu:/usr/bin# sudo apt-get install python3-pip
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-pip is already the newest version (8.1.1-2ubuntu0.4).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@VM-0-8-ubuntu:/usr/bin# pip3 -V
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ModuleNotFoundError: No module named 'pip'
root@VM-0-8-ubuntu:/usr/bin# sudo apt updaate
E: Invalid operation updaate
root@VM-0-8-ubuntu:/usr/bin# sudo apt update
Hit:1 http://mirrors.tencentyun.com/ubuntu xenial InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu xenial-security InRelease                 
Ign:3 http://mirrors.aliyun.com/ubuntu trusty InRelease                              
Hit:4 http://mirrors.tencentyun.com/ubuntu xenial-updates InRelease     
Hit:5 http://mirrors.aliyun.com/ubuntu trusty-security InRelease        
Hit:6 http://mirrors.aliyun.com/ubuntu trusty-updates InRelease                   
Hit:7 http://mirrors.aliyun.com/ubuntu trusty-proposed InRelease                  
Hit:8 http://mirrors.aliyun.com/ubuntu trusty-backports InRelease                 
Hit:9 http://mirrors.aliyun.com/ubuntu trusty Release                             
Reading package lists... Done 
Building dependency tree       
Reading state information... Done
All packages are up to date.
root@VM-0-8-ubuntu:/usr/bin# sudo apt upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@VM-0-8-ubuntu:/usr/bin# sudo apt-get install python3-pip
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-pip is already the newest version (8.1.1-2ubuntu0.4).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@VM-0-8-ubuntu:/usr/bin# pip3 -V
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ModuleNotFoundError: No module named 'pip'
root@VM-0-8-ubuntu:/usr/bin#

【问题讨论】:

  • 问题是什么?

标签: ubuntu pip python-3.7


【解决方案1】:

它可能坏了。你可以试试

sudo apt purge python3-pip  
sudo apt install python3-pip

如果这不起作用,您可能使用的是未打包的 pip 版本,因此 pip main 可能已移至 pip._internal。在您的 pip3 文件中将 from pip import main 更改为 from pip._internal import main。您可以通过在终端中输入 which pip3 来查找 pip3 文件位置。

【讨论】:

    【解决方案2】:

    pip 在 Ubuntu 中与 Python 分开打包(python-pippython3-pip)。 Python 3.7.2 无法识别您的 pip3 版本(版本 8.1.1),它不是来自默认的 Ubuntu 16.04 存储库。因此,您必须强制重新安装 pip3。打开终端并输入:

    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py  
    python3 get-pip.py --force-reinstall     
    

    检查 pip3 的版本以确保它是最新版本。

    $ pip3 -V
    点 19.0.2

    Python 3 无法找到 pip3 的原因是您的软件源的错误状态,您应该立即修复,否则此类错误将不断重复发生。你不应该混合来自两个不同版本的 Ubuntu 的软件源,在你的情况下是 xenial (16.04) 和 trusty (14.04)。修复软件源的最简单方法是简单地将 /etc/apt/sources.list 中当前拥有的任何内容替换为默认的 Ubuntu 16.04 软件源。标准的 Ubuntu 16.04 sources.list 如下所示:

    Ubuntu 16.04 的标准 sources.list 文件如下所示:

    deb http://archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse  
    deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
    deb http://security.ubuntu.com/ubuntu xenial-security main restricted universe multiverse  
    deb http://archive.canonical.com/ubuntu xenial partner  
    

    在 nano 文本编辑器中打开 /etc/apt/sources.list 进行编辑:

    sudo nano /etc/apt/sources.list  
    

    另一种方法是注释掉/etc/aptsources.list 中包含字符串trusty 的所有行,方法是在包含字符串trusty 的每一行前面加上一个# 字符。

    使用 nano 的说明始终显示在页面底部。使用键盘组合 Ctrl + O 然后按 Enter 将文件保存到当前位置。使用键盘组合 Ctrl + X 退出 nano。

    修复完sources.list 后,运行sudo apt update 刷新可用包列表。您还可以在软件和更新应用程序的 Ubuntu 软件 选项卡中将默认镜像切换为离您更近的镜像。这样软件安装和更新下载速度会更快。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-23
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 2020-04-12
      • 2018-10-02
      相关资源
      最近更新 更多