我们现在需要将某个环境已经安装的 python 包离线传递到另外一个环境,且确保这种安装行为最终不需要对 PYPI 中央仓库的有效连接,也能完成。下面给出两种办法:
docker container
如果你的 python 环境位于某个容器内,那最好的办法就是执行docker commit
操作构建镜像:
bash
docker commit <容器id> <自定义的镜像名称>:<tag>
比如
bash
docker commit 123 456:78
tag不是必须的,若没有指定,默认为latest
。容器id可以通过docker ps
获取:
bash
123 456 "789" 11 minutes ago Up 11 minutes 0123
其中的123
就是容器id
执行docker commit成功构建镜像以后,就可以用docker save
保存镜像:
bash
docker save -o <tar包名称>.tar <自定义的镜像名称>:<tag>
这一步可能存在交换分区不足,导致无法保存镜像的问题,报错如下:
bash
Error response from daemon: write layer.tar: no space left on device
可以通过以下命令获取docker交换空间的地址:
bash
docker info | grep "Docker Root Dir"
至于空间不足,无外乎删一点东西,或者重定向到其他位置,或者修改 docker 的配置。 这些修改大部分需要重启 docker 进程,在生产环境下慎用。另外也可以在参数中指定中间位置,但是仅限高版本的 docker
转移 python 包
如果你不是在容器环境内,那操作可以复杂一点。
导出
首先需要导出 python 包的清单文件(requirements.txt):
bash
pip freeze > requirements.txt
根据清单文件在源环境下载包到本地:
bash
pip download -r requirements.txt -d python-pkgs/ --no-deps
下载的包有whl文件,也有压缩包,这是正常现象。
源环境已经安装的包,有可能会互相冲突。也就是源环境的pip生态有可能已经依赖不自恰了。
我们需要先去掉清单文件的版本号,反正仅限本地目录安装,也装不了别的版本。但根本目的是让pip无法针对版本号进行依赖性检查:
python
import argparse
import re
from pathlib import Path
def remove_version_specifiers(input_file, output_file=None, inplace=False):
"""
从requirements文件中移除所有包的版本约束
参数:
input_file (str): 输入的requirements文件路径
output_file (str): 输出文件路径,默认为None(与输入文件同名但添加-cleaned后缀)
inplace (bool): 是否直接在原文件上修改
"""
# 读取文件内容
with open(input_file, 'r', encoding='utf-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_part
else:
code_part = line
comment_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_file
elif output_file:
output_path = output_file
else:
input_path = Path(input_file)
output_path = input_path.with_name(f"{input_path.stem}-cleaned{input_path.suffix}")
# 写入清理后的内容
with open(output_path, 'w', encoding='utf-8') as f:
f.writelines(cleaned_lines)
print(f"已成功清理文件: {input_file} → {output_path}")
return output_path
if __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', action='store_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包,需要全卸载掉,防止和从源环境导入的冲突。
在目标环境导出清单文件:
bash
pip freeze > u-requirements.txt
然后卸载掉全部的依赖:
bash
pip uninstall -r u-requirements.txt -y
此时将之前复制过来的离线文件全部安装即可:
bash
pip install --no-index --find-links=python-pkgs/ -r requirements-cleaned.txt --only-binary=:none: --no-build-isolation --no-deps
--no-index
和--find-links=python-pkgs/
的组合,可以让pip只从本地的目录里获取要安装的包。
--no-deps
会使 pip 在安装软件包时,不安装它所依赖的其他包。也就是说,只会安装requirements-cleaned.txt
文件里直接列出的软件包。这是为了防止在安装的时候出现依赖冲突。
--no-build-isolation
:正常情况下,pip 在构建软件包时会创建一个隔离的环境。而使用这个参数后,就不会创建隔离环境,构建过程会依赖当前环境里已有的依赖项。这是防止 pip 无法检测到本地已经安装的setup-tools
。
--only-binary=:none::
这个参数表明不使用预编译的二进制包(像.whl 文件)进行安装.