1.先打包环境配置
如果是conda:
conda activate your_env
conda env export > environment.yml
如果是 pip
pip freeze > requirements.txt
2.镜像使用
(1)win上没有打包镜像的软件,我们要先在windows上下载WSL(类似于虚拟机)
直接在终端
wsl --install
(2)重启,在应用商店下载ubuntu
打开ubuntu,即可进入Linux shell
(3)下载singularity
apt install singularity-container
(4)打包镜像
sudo -E singularity build python_env.sif /D:/study/brain_age/python.def
注意,这并非是linux可以识别的路径,因此改成:
/mnt/d/study/brain_age/python.def
def文件如下:
bash
Bootstrap: docker
From: docker.1panel.live/library/ubuntu:22.04
%files #复制自己的yml
/mnt/d/study/brain_age/environment.yml /opt/environment.yml
%post
# 1. 替换 Ubuntu APT 源为阿里云镜像
sed -i 's@//.*archive.ubuntu.com@//mirrors.aliyun.com@g' /etc/apt/sources.list
sed -i 's@//.*security.ubuntu.com@//mirrors.aliyun.com@g' /etc/apt/sources.list
# 2. 安装基础依赖
apt-get update && apt-get install -y wget bzip2 ca-certificates git
apt-get clean
# 3. 下载并安装 Miniconda (使用清华大学镜像源下载安装包)
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /miniconda.sh
bash /miniconda.sh -b -p /opt/conda
#rm /miniconda.sh
# 使用路径调用 conda 并根据 yml 文件更新 base 环境,显式accept tos
/opt/conda/bin/conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main
/opt/conda/bin/conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r
/opt/conda/bin/conda env update -n base -f /opt/environment.yml
/opt/conda/bin/conda clean -afy
%environment
# 运行时环境变量设置
export PATH="/opt/conda/bin:$PATH"
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
%runscript
# 容器默认执行 python
exec python "$@"
%help
这是一个针对国内网络环境优化的 Python 深度学习环境镜像。
包含 Conda、APT 镜像源加速。
最后,怎么查询wsl里的文件?可参考
https://blog.csdn.net/muzizongheng/article/details/105286529