丹东市网站建设,网站运营维护措施有哪些,做数码测评的网站,app运营费用预算我们现在需要将某个环境已经安装的 python 包离线传递到另外一个环境#xff0c;且确保这种安装行为最终不需要对 PYPI 中央仓库的有效连接#xff0c;也能完成。下面给出两种办法#xff1a;
docker container
如果你的 python 环境位于某个容器内#xff0c;那最好的办法…我们现在需要将某个环境已经安装的 python 包离线传递到另外一个环境且确保这种安装行为最终不需要对 PYPI 中央仓库的有效连接也能完成。下面给出两种办法
docker container
如果你的 python 环境位于某个容器内那最好的办法就是执行docker commit操作构建镜像
docker commit 容器id 自定义的镜像名称:tag比如
docker commit 123 456:78tag不是必须的若没有指定默认为latest。容器id可以通过docker ps 获取
123 456 789 11 minutes ago Up 11 minutes 0123 其中的123就是容器id
执行docker commit成功构建镜像以后就可以用docker save保存镜像
docker save -o tar包名称.tar 自定义的镜像名称:tag这一步可能存在交换分区不足导致无法保存镜像的问题报错如下
Error response from daemon: write layer.tar: no space left on device可以通过以下命令获取docker交换空间的地址
docker info | grep Docker Root Dir至于空间不足无外乎删一点东西或者重定向到其他位置或者修改 docker 的配置。 这些修改大部分需要重启 docker 进程在生产环境下慎用。另外也可以在参数中指定中间位置但是仅限高版本的 docker
转移 python 包
如果你不是在容器环境内那操作可以复杂一点。
导出
首先需要导出 python 包的清单文件requirements.txt
pip freeze requirements.txt根据清单文件在源环境下载包到本地
pip download -r requirements.txt -d python-pkgs/ --no-deps下载的包有whl文件也有压缩包这是正常现象。
源环境已经安装的包有可能会互相冲突。也就是源环境的pip生态有可能已经依赖不自恰了。
我们需要先去掉清单文件的版本号反正仅限本地目录安装也装不了别的版本。但根本目的是让pip无法针对版本号进行依赖性检查
import argparse
import re
from pathlib import Pathdef remove_version_specifiers(input_file, output_fileNone, inplaceFalse):从requirements文件中移除所有包的版本约束参数:input_file (str): 输入的requirements文件路径output_file (str): 输出文件路径默认为None与输入文件同名但添加-cleaned后缀inplace (bool): 是否直接在原文件上修改# 读取文件内容with open(input_file, r, encodingutf-8) as f:lines f.readlines()# 定义正则表达式模式匹配包名和版本约束pattern re.compile(r^([^\s!#])([!].*)?$)cleaned_lines []for line in lines:line line.strip()# 跳过空行和注释if not line or line.startswith(#):cleaned_lines.append(line \n)continue# 处理带有注释的行if # in line:code_part, comment_part line.split(#, 1)code_part code_part.strip()comment_part # comment_partelse:code_part linecomment_part # 移除版本约束match pattern.match(code_part)if match:package_name match.group(1)cleaned_lines.append(f{package_name}{comment_part}\n)else:# 如果不匹配标准格式保留原样cleaned_lines.append(f{line}\n)# 确定输出文件路径if inplace:output_path input_fileelif output_file:output_path output_fileelse:input_path Path(input_file)output_path input_path.with_name(f{input_path.stem}-cleaned{input_path.suffix})# 写入清理后的内容with open(output_path, w, encodingutf-8) as f:f.writelines(cleaned_lines)print(f已成功清理文件: {input_file} → {output_path})return output_pathif __name__ __main__:parser argparse.ArgumentParser(description移除requirements文件中的版本约束)parser.add_argument(input_file, help输入的requirements文件路径)parser.add_argument(-o, --output, help输出文件路径默认为输入文件名添加-cleaned后缀)parser.add_argument(-i, --inplace, actionstore_true, help直接在原文件上修改)args parser.parse_args()try:remove_version_specifiers(args.input_file, args.output, args.inplace)except Exception as e:print(f处理文件时出错: {e}) 现在就可以将清单文件和包一起复制到目标环境了。
导入
如果目标环境有已经安装的python包需要全卸载掉防止和从源环境导入的冲突。
在目标环境导出清单文件
pip freeze u-requirements.txt然后卸载掉全部的依赖
pip uninstall -r u-requirements.txt -y此时将之前复制过来的离线文件全部安装即可
pip install --no-index --find-linkspython-pkgs/ -r requirements-cleaned.txt --only-binary:none: --no-build-isolation --no-deps--no-index和--find-linkspython-pkgs/的组合可以让pip只从本地的目录里获取要安装的包。
--no-deps会使 pip 在安装软件包时不安装它所依赖的其他包。也就是说只会安装requirements-cleaned.txt文件里直接列出的软件包。这是为了防止在安装的时候出现依赖冲突。
--no-build-isolation正常情况下pip 在构建软件包时会创建一个隔离的环境。而使用这个参数后就不会创建隔离环境构建过程会依赖当前环境里已有的依赖项。这是防止 pip 无法检测到本地已经安装的setup-tools。
--only-binary:none:这个参数表明不使用预编译的二进制包像.whl 文件进行安装.