首页 > 解决方案 > pip3 -V 应该打印 pip3 版本,但在 Ubuntu 16.04 中返回 No module named 'pip'

问题描述

我在 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#

标签: ubuntupippython-3.7

解决方案


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  

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

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

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


推荐阅读