栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python pip下载本地依赖包,并在离线环境中安装,并解决报错ERROR: Could not find a version that satisfies the requirement报错

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python pip下载本地依赖包,并在离线环境中安装,并解决报错ERROR: Could not find a version that satisfies the requirement报错

文章目录
  • 步骤1:导出依赖
  • 步骤2:离线下载依赖包
  • 步骤3:进入新环境使用python安装依赖

整体思路如下:

  1. 首先根据项目需要导出依赖包,由于本地的python环境中其实安装了很多乱七八糟的包,这些包不一定都用得上,因此只需要导出必要的依赖就可以
  2. 根据当前项目的依赖清单,下载对应的安装包,以便在新的python环境中可以直接离线安装这些
  3. 进入到新环境中离线安装即可
步骤1:导出依赖

首先在当前环境下安装:pip install pipreqs,这个库可以帮助你筛选出项目需要的python包,而不是当前环境的全部依赖。安装完成后使用命令:

cd 项目根目录/
pipreqs ./ --encoding=utf-8

在根目录下会生成requirements.txt文件,我的内容如下:

itemadapter==0.1.0
selenium==3.141.0
Scrapy==2.3.0
python_dateutil==2.8.2
........
步骤2:离线下载依赖包

然后根据requirements.txt导出需要的安装包

pip download -d PIPDIR -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com 
  • -d指定导出的文件夹
  • -r指定根据哪一个文件导出
  • -i表示使用阿里源(当然可以使用其他国内源啊)
  • --trusted-host表示信任主机
  • 更多配置请参考:https://pip.pypa.io/en/stable/cli/pip_download/#options

有几个常用配置:

  • –platform:指定需要安装的平台,比如:linux_x86_64,如果在windows/mac上默认会下载windows/mac的安装包,在linux上是肯定安装不了的
  • –python-version:python的版本,默认与当前环境相同,如果值为3,则python版本为3.0.0, 若值为3.7,则python版本为3.7.0或3.7.3,最好根据python --version指定完整的版本号

注意:这里一定要添加--trusted-host,否则会报错:

WARNING: The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.aliyun.com'.
ERROR: Could not find a version that satisfies the requirement xxx==xxx (from versions: none)
ERROR: No matching distribution found for xxx==xxx
步骤3:进入新环境使用python安装依赖

根据自己的实际情况,把这个文件夹和requirements.txt移动到新的环境中,然后使用:

pip install --no-index --find-links=PIPDIR -r requirements.txt
  • --find-links就是存放安装文件的目录
  • -r是指按照requirements.txt这个文件去安装文件目录中找需要的安装包
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/294836.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号