pip包管理器使用及如何离线安装第三方依赖

1. pip 介绍

  • pip 是通用的 Python 包管理工具

  • pip 提供了对 Python 包的查找、下载、安装、卸载功能

  • pip 已内置于 Python 3.42.7 及以上版本,其他版本需另行安装

  • 常使用 pip 命令来安装和卸载 Python 的第三方库

2. pip 命令

命令 描述
help 帮助显示命令的帮助
config 配置本地和全局变量(常用配置仓库的镜像地址)
list 列表列出已安装的包
install 安装包安装
uninstall 卸载卸载包
freeze 冻结按需求格式安装的包的输出
check 检查已安装的软件包是否具有兼容的依赖项
show 显示已安装软件包的信息
download 下载下载包
search 搜索PyPI查找包
wheel 根据您的需求构建轮子
hash 包存档的哈希计算值
completion 用于命令完成的辅助命令
debug 显示对调试有用的信息

3. pip 使用示例

  • 查看版本和路径
pip --version
  • 升级pip
pip install --upgrade pip
pip install -U pip
  • 帮助,查看命令的选项
pip help
  • pip设置镜像源

    • 临时更换下载源,下载时可以临时将下载源修改以上某个下载源,方法如下:

      下载 requests 库时临时使用清华源进行下载
      pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
      
      下载 requests 库时临时使用豆瓣源进行下载
      pip install -i http://pypi.douban.com/simple/ requests
      
    • 永久更换下载源,不想每次下载第三方库时都要指定下载源地址,使用如下方法进行修改

      • linux
        修改 ~/.pip/pip.conf (没有就创建一个), 内容如下:

        [global]
        index-url = https://pypi.tuna.tsinghua.edu.cn/simple
        
      • windows
        直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,在pip 目录下新建文件pip.ini,添加下面的代码或者按快捷键 win+R 打开用户目录%HOMEPATH%,在此目录下创建 pip 文件夹,在 pip 目录下创建 pip.ini 文件,内容如下

        [global] 
        timeout = 6000 
        index-url = https://pypi.tuna.tsinghua.edu.cn/simple 
        trusted-host = pypi.tuna.tsinghua.edu.cn
        
        pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
      

http://pypi.douban.com/simple/ 豆瓣
http://mirrors.aliyun.com/pypi/simple/ 阿里
http://pypi.hustunique.com/simple/ 华中理工大学
http://pypi.sdutlinux.org/simple/ 山东理工大学
http://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学
https://pypi.tuna.tsinghua.edu.cn/simple/ 清华大学
https://pypi.hustunique.com/ 华中科技大学
http://mirrors.cloud.tencent.com/pypi/simple 腾讯
https://repo.huaweicloud.com/repository/pypi/simple/ 华为

  • 取消全局配置
pip config unset global.index-url
  • 查看镜像列表
pip config list
  • 查看已经安装的包的名字和版本号
pip list
  • 查看哪些包可以升级
pip list -o
  • pip search命令会报错,需要先安装pip-search,再使用pip-search
pip install pip-search
pip-search requests
  • 安装最新版本的库
pip install 包名
  • 安装指定版本的库
pip install 包名==2.28.1
  • 安装最小版本(通过使用==, >=, <=, >, < 来指定一个版本号)
pip install '包名>=2.28.1'
  • 升级版本号(通过使用==, >=, <=, >, < 来指定一个版本号)
pip install --upgrade 包名
  • 卸载库
pip uninstall 包名
  • 下载某个包但不安装包
pip download 包名 -d "下载路径"
  • 检查安装的包是否有版本冲突
pip check
  • 检查某个包是否有版本冲突
pip check 包名
  • 查看某个库的详情
pip show -f 包名
  • 将项目中的所有第三方包的包名和版本号写入到requirements.txt文件中
pip freeze > requirements.txt
  • 批量安装requirements.txt文件中的列举的所有包
pip install -r requirements.txt

4. 离线安装python依赖

4.1 安装pipreqs包

pip install pipreqs

4.2 生成requirements.txt文件

在项目文件路径下

pipreqs ./ 

如果编码报错,指定编码

pipreqs ./ --encoding=utf8

4.3 导出依赖包

根据依赖列表,导出包到本地的 offline 目录

pip download -d offline -r .\requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

4.4 安装离线包

pip install --no-index --find-links=offline -r requirements.txt 

注意:对于离线安装Python的依赖,Python版本最好一致,不然会出现依赖包安装失败的问题,踩过坑。

猜你喜欢

转载自blog.csdn.net/zxd1435513775/article/details/127184243
今日推荐