Python如何离线移植依赖包

425 阅读1分钟

在项目中会遇到这种情况,本地或测试环境开发完,想把py项目移植到生产环境,但是生产是断网环境,所以需要离线移植相关依赖。作为Java工程师的我业务中也使用到了同事的py包,需要特殊处理下🤣。

现有环境导出

导出依赖目录到文件

pip freeze > requirements.txt

下载requirements.txt中的包到本地

pip download --platform linux_x86_64 --no-deps -r requirements.txt -d /tmp/paks/

# --platform linux_x86_64 指定下载的是Linux环境的包 因为本地可能是windows、MAC OS 但是服务器是Linux
# -d 指定下载保存的目录

如果出现下面错误:

在后面加上参数 --trusted-host mirrors.aliyun.com

pip download --platform linux_x86_64 --no-deps -r requirements.txt -d /tmp/paks/ --trusted-host mirrors.aliyun.com 

导入新环境

将本地下载的paks文件的依赖拷贝到服务器

安装

pip install --no-index --find-links=/home/pyenv/paks -r requirements.txt 

# /home/pyenv是拷贝到的文件夹

文章收录在 JavaDeveloperBrain ,更多【Java学习内容】可以访问仓库~~