[TOC]
0x00 前言简述
描述: 在Python中默认的包、模块管理工具是 pip, 使得其可以对 Python 包的查找、下载、安装、卸载的功能。
0x01 pip 命令 - Python 包管理工具
安装配置
pip 安装
描述: python 依赖库建议使用pip进行管理, 如果你的没有安装pip可执行如下命令进行安装:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20# Python 环境通用安装
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate && python get-pip.py
python get-pip.py # 安装 python 2.7 的 pip
python3 get-pip.py # 安装 python 3.7 的 pip3
# Ubuntu、Kail 发行版安装
apt-get install python-pip
apt-get install python-setuptools
# Python 默认方式安装
easy_install pip
# 安装指定版本的pip
python -m pip install pip==22.1.1
# 版本查看
pip --version
# pip 22.1.1 from D:\Study\Python\lib\site-packages\pip (python 3.8)
pip3 --version
# pip 22.1.1 from /usr/local/python3.8/lib/python3.8/site-packages/pip (python 3.8)
温馨提示:pip 已内置于Python3.4和2.7及以上版本,其他版本需另行安装。
pip 常用命令1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16Usage: pip [options]
Commands:
install # 安装包安装 (Install packages.)
download # 下载下载包 (Download packages.)
uninstall # 卸载卸载包 (Uninstall packages.)
freeze # 冻结按需求格式安装的包的输出 (Output installed packages in requirements format.)】
list # 列表列出已安装的包 ( List installed packages.)】
show # 显示已安装软件包的信息 ( Show information about installed packages.)】
check # 检查已安装的软件包是否具有兼容的依赖项 ( Verify installed packages have compatible dependencies.)】
config # 配置管理本地和全局配置 ( Manage local and global configuration.)】
search # 搜索PyPI查找包 (Search PyPI for packages.)】
wheel # 根据您的需求构建轮子 (Build wheels from your requirements.)】
hash # 包存档的哈希计算值 ( Compute hashes of package archives.)】
completion # 用于命令完成的辅助命令 ( A helper command used for command completion.)】
debug # 显示对调试有用的信息 ( Show information useful for debugging.)】
help # 帮助显示命令的帮助 (Show help for commands.)
pip 镜像加速配置
pypi 国内镜像源地址:
- 阿里云: http://mirrors.aliyun.com/pypi/simple/
- 豆瓣(douban): http://pypi.douban.com/simple/
- 清华大学: https://pypi.tuna.tsinghua.edu.cn/simple/
- 中国科技大学: https://pypi.mirrors.ustc.edu.cn/simple/
- 中国科学技术大学: http://pypi.mirrors.ustc.edu.cn/simple/
镜像源配置
方式1.命令行全局配置默认镜像源.
1
2
3
4
5
6
7# Windows
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# Writing to C:\Users\WeiyiGeek\AppData\Roaming\pip\pip.ini
# Linux
pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/
# Writing to /root/.config/pip/pip.conf方式2.命令行参数配置临时镜像源.
1
2
3
4
5
6
7
8# 升级 pip 到最新的版本 (>=10.0.0) 后进行配置
pip install pip -U
# 如果上面升级连接的默认源网络连接较差,临时使用本条命令来升级 pip;
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
# 临时生效
pip install <安装包库名称> -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install <安装包库名称> -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip --default-timeout=100 install 库名称 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com方式3.pip 配置文件方式配置.
1
2
3$ vim /root/.config/pip/pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/方式4.第三方IDE软件配置,此处 pycharm 为例配置镜像仓库后,安装速度将会比较快。

WeiyiGeek.配置pycharm镜像仓库
pip 使用实践
实践实例
1 | # 1.常规使用下载安装easyocr包或者从requ |
pip 错误记录
问题1:使用pip安装包模块C++运行库报错
- 错误信息: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”: https://visualstudio.microsoft.com/downloads/
1
2Python 2.7 需要Microsoft Visual C++ 9.0的库
Python 3.6 需要Microsoft Visual C++ 14.0的库
问题2:centos下安装pip时失败的解决方法?1
2
3
4
5
6
7
8
9
10
11
12#解决方法1:先安装扩展源EPEL,EPEL(http://fedoraproject.org/wiki/EPEL) 是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。
#首先安装epel扩展源:
$ sudo yum -y install epel-release
#然后再安装pip
$ sudo yum -y install python-pip
#解决方法2:如过没有安装pip, 可执行如下命令进行安装:
wget https://sec.ly.com/mirror/get-pip.py --no-check-certificate && python get-pip.py
#更新到pip最新版本:
$ pip install -U pip
$ python -m pip install --upgrade pip
问题3:输入pip命令报错:from pip import main ImportError: cannot import name ‘main’
- 错误信息:
1
2
3
4
5workspace> pip
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
from pip import main
ImportError: cannot import name main - 问题原因: 由于在ubuntu 16.04上升级pip版本后导致的:
- 修复流程:
1
2
3
4
5
6
7
8
9
10# 1.若在只读权限下强制保存会导致文件受损,建议修改配置文件时先查看是否具有权限
$ vim /usr/bin/pip
from pip import __main__ #这行也要修改
if __name__ == '__main__':
sys.exit(__main__._main())#增加__main__._
# 2.最后保存查看
workspace> pip -V
pip 19.1.1 from /home/coding/.local/lib/python2.7/site-packages/pip (python 2.7)
问题4:安装BeautifulSoup失败
原因分析:
- 1) 可能是PIP版本过低造成安装不成功,目前使用版本10.0.1,已有最新版本18.1;
- 2) 也可能是安装包不对,需要使用BeautifulSoup4的包进行安装,不升级PIP的情况下,进行安装BeautifulSoup4包成功。
问题5:采用pip3安装Certbot出错
- 问题描述:
1
2Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_root/cryptography
Storing debug log for failure in /root/.pip/pip.log - 解决办法:
1
2easy_install -U setuptools
sudo pip3 install certbot
问题6: setuptools版本太老导致依赖无法下载
- 问题描述: RuntimeError: cryptography requires setuptools 18.5 or newer, please upgrade to a newer version of setuptool.
- 解决办法:
pip install --upgrade setuptools
问题7.执行pip命令出现ModuleNotFoundError: No module named 'pip'
错误
- 问题原因: 系统中没有安装pip模块
- 解决办法: 安装和升级pip模块
1
2
3python -m ensurepip
python -m pip install --upgrade pip
python -m pip install pip==19.0.3 # 安装指定版本
问题8.在Windows中使用pip安装某个库的时候,出现如图错误提示WARNING: Ignoring invalid distribution -ip
- 错误原因: 在之前下载某个库的时候没有成功或者中途异常退出导致。
- 解决方法:我们进入到Python安装目录,例如我的
D:\Program Files (x86)\Python37-32\Lib\site-packages
,删除~ip开头的目录,然后pip重新安装库即可。

WeiyiGeek.pip安装时错误