推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
MasterCai
V2EX  ›  Python

[请教]miniconda 环境迁移最佳实践

  •  
  •   MasterCai · Aug 1, 2023 · 2707 views
    This topic created in 1189 days ago, the information mentioned may be changed or developed.

    miniconda 应该是目前比较主流的 python 包管理工具(deep learning 领域),但是其环境迁移一直很难做。

    先不说跨平台的迁移,就说在同一平台下(如 ubuntu-x64),使用conda env export导出配置文件后,很多情况下都无法在另一台机器上成功配置。很多时候都只能手动处理,非常繁琐。而且一台设备的环境变化之后,其他设备也没法快速同步,只能手动更新(没有及时安装的话后面可能就忘记了,导致出现问题)。

    想请教一下大家都是如何处理 conda 的环境迁移问题的?

    7 replies  •  2023-08-01 22:23:31 +08:00
    yzbythesea
    yzbythesea
    Aug 1, 2023
    1
    pyenv
    ConfusedBiscuit
    ConfusedBiscuit
    Aug 1, 2023
    2
    docker
    dif
    dif
    Aug 1, 2023❤️1
    3
    conda-pack
    ruanimal
    ruanimal
    Aug 1, 2023❤️1
    4
    直接复制 conda 目录,执行下面的脚本重写一些路径就行了

    #!/bin/sh

    if [[ -z $1 ]]; then
    echo usage: $0 conda/bin/conda
    exit
    fi

    set -x -e

    conda_exe=$(realpath $1)
    CONDA_DIR=$(dirname $(dirname $conda_exe))

    OLD_PREFIX=$(grep CONDA_EXE $CONDA_DIR/etc/profile.d/conda.sh | head -n1 | cut -d "'" -f 2 | sed 's|/bin/conda||')
    NEW_PREFIX=$(echo $conda_exe | sed 's|/bin/conda||')

    need_sed=$(grep -RnIH "$OLD_PREFIX" $NEW_PREFIX 2>/dev/null | cut -d ':' -f 1 | sort | uniq)
    sed -i "s|$OLD_PREFIX|$NEW_PREFIX|g" $need_sed

    need_sed=$(grep -r -E '^#\!.*python' $NEW_PREFIX/bin/ | cut -d ':' -f 1)
    sed -i "1c #!${NEW_PREFIX}/bin/python" $need_sed
    masellum
    masellum
    Aug 1, 2023❤️1
    5
    这事我之前干过,用 `conda env export --from-history` 导出一份 environment.yml ,然后注意里面的 name 和 prefix 按自己的需要改动一下,可能会有缺的 channel 记得补一下,用 pip 装的包可以在 pip 这个 entry 下面写;这样基本上可以良好地迁移
    MasterCai
    MasterCai
    OP
    Aug 1, 2023
    6
    @ruanimal 这方法好暴力:D 不过感觉同平台应该没啥问题
    raycool
    raycool
    Aug 1, 2023
    7
    conda-pack 可行
    直接目标机器上 source activate 即可激活环境。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1561 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 23:04 · PVG 07:04 · LAX 15:04 · JFK 18:04
    ♥ Do have faith in what you're doing.