
描述: 在Python中默认的包、模块管理工具是 pip, 使得其可以对 Python 包的查找、下载、安装、卸载的功能。
pip 安装 描述: python 依赖库建议使用pip进行管理, 如果你的没有安装pip可执行如下命令进行安装:
# 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 常用命令
Usage: 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.)pypi 国内镜像源地址:
镜像源配置
方式1.命令行全局配置默认镜像源.
# 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.命令行参数配置临时镜像源.
# 升级 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 配置文件方式配置.
$ vim /root/.config/pip/pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/方式4.第三方IDE软件配置,此处 pycharm 为例配置镜像仓库后,安装速度将会比较快。

WeiyiGeek.配置pycharm镜像仓库
实践实例
# 1.常规使用下载安装easyocr包或者从requ
pip install easyocr
# 安装最新flask包, -i 指定仓库镜像源, --no-cache-dir 表示不生产缓存目录。
pip install flask --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple
# 从 requirements.txt 文件中读取要安装下载的包,-r 指定依赖包列表的存储文件
pip install -r requirements.txt
# 2.查看 pip 支持的包类型
pip debug --verbose
# pip version: pip 22.1.1 from D:\Study\Python\lib\site-packages\pip (python 3.8)
# sys.version: 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
# sys.executable: D:\Study\Python\python.exe
# sys.getdefaultencoding: utf-8
# sys.getfilesystemencoding: utf-8
# locale.getpreferredencoding: cp936
# sys.platform: win32
# 3.查看系统所有安装的依赖包
pip list
# 4.查看并列举出虚拟环境新安装的包
pip freeze
pip freeze > requirements.txt
# WARNING: Ignoring invalid distribution -ip (d:\study\python\lib\site-packages)
# certifi==2022.5.18.1
# charset-normalizer==2.0.12
# click==8.0.3
# colorama==0.4.4
# cycler==0.10.0
# easyocr @ file:///G:/%E8%BF%85%E9%9B%B7%E4%B8%8B%E8%BD%BD/easyocr-1.4.2-py3-none-any.whl问题1:使用pip安装包模块C++运行库报错
错误信息: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”: https://visualstudio.microsoft.com/downloads/
Python 2.7 需要Microsoft Visual C++ 9.0的库
Python 3.6 需要Microsoft Visual C++ 14.0的库问题2:centos下安装pip时失败的解决方法?
#解决方法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’
错误信息:
workspace> 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.若在只读权限下强制保存会导致文件受损,建议修改配置文件时先查看是否具有权限
$ 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失败 原因分析:
问题5:采用pip3安装Certbot出错
问题描述:
Command 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解决办法:
easy_install -U setuptools
sudo pip3 install certbot问题6: setuptools版本太老导致依赖无法下载
pip install --upgrade setuptools问题7.执行pip命令出现ModuleNotFoundError: No module named 'pip'错误
问题原因: 系统中没有安装pip模块
解决办法: 安装和升级pip模块
python -m ensurepip
python -m pip install --upgrade pip
python -m pip install pip==19.0.3 # 安装指定版本问题8.在Windows中使用pip安装某个库的时候,出现如图错误提示WARNING: Ignoring invalid distribution -ip
D:\Program Files (x86)\Python37-32\Lib\site-packages,删除~ip开头的目录,然后pip重新安装库即可。
WeiyiGeek.pip安装时错误