V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 271 天前 · 1169 次点击
    这是一个创建于 271 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

    7 条回复    2023-08-01 22:23:31 +08:00
    yzbythesea
        1
    yzbythesea  
       271 天前
    pyenv
    ConfusedBiscuit
        2
    ConfusedBiscuit  
       271 天前
    docker
    dif
        3
    dif  
       271 天前   ❤️ 1
    conda-pack
    ruanimal
        4
    ruanimal  
       271 天前   ❤️ 1
    直接复制 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
        5
    masellum  
       271 天前   ❤️ 1
    这事我之前干过,用 `conda env export --from-history` 导出一份 environment.yml ,然后注意里面的 name 和 prefix 按自己的需要改动一下,可能会有缺的 channel 记得补一下,用 pip 装的包可以在 pip 这个 entry 下面写;这样基本上可以良好地迁移
    MasterCai
        6
    MasterCai  
    OP
       271 天前
    @ruanimal 这方法好暴力:D 不过感觉同平台应该没啥问题
    raycool
        7
    raycool  
       271 天前
    conda-pack 可行
    直接目标机器上 source activate 即可激活环境。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1072 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:45 · PVG 02:45 · LAX 11:45 · JFK 14:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.