简体   繁体   English

从 conda 环境导出顶级依赖项,包括 pip 安装

[英]Exporting top level dependencies from conda environment including pip installations

If I do conda env export --from-history -f environment.yml I get all top level dependencies I installed but I don't get packages I installed via pip .如果我执行conda env export --from-history -f environment.yml我会得到我安装的所有顶级依赖项,但我不会得到我通过pip安装的包。 If on the other hand, I use conda env export -f environment.yml , I get the pip packages along with all of the dependencies (rather than only the top level ones).另一方面,如果我使用conda env export -f environment.yml ,我会获得pip包以及所有依赖项(而不仅仅是顶级的)。 Is there a way to export so that I get top level dependencies, including pip packages?有没有办法导出以便我获得顶级依赖项,包括pip包?

Nothing in the current CLI can do this.当前的 CLI 中没有任何东西可以做到这一点。

As @Peter mentioned, it's likely simplest just to export both and manually copy over the - pip: section.正如@Peter 提到的,最简单的方法可能是导出两者并手动复制- pip:部分。 Another option is to do the --from-history export and then a pip freeze > requirements.txt .另一种选择是执行--from-history导出,然后pip freeze > requirements.txt The requirements.txt can then be added as in this answer .然后可以添加requirements.txt如本答案所示

Solution from @ gwerbin来自@gwerbin 的解决方案

I found a script work for this purpose recently, here is the link of the codes.我最近为此目的找到了一个脚本,这是代码的链接。 conda_env_export.py conda_env_export.py


As the codes are short, I could paste the code below.由于代码很短,我可以粘贴下面的代码。 Note that I modify some places for my personal usage.请注意,我修改了一些地方供我个人使用。

import re
import subprocess
import sys
import yaml
import fire


CONDA_PATH = '/home/ubuntu/miniconda3/condabin/conda'


def export_env(history_only=False):
    cmd = [CONDA_PATH, 'env', 'export']
    if history_only:
        cmd.append('--from-history')

    cp = subprocess.run(cmd, stdout=subprocess.PIPE)
    try:
        cp.check_returncode()
    except:
        raise
    else:
        return yaml.safe_load(cp.stdout)


def _is_history_dep(d, history_deps):
    if not isinstance(d, str):
        return False
    d_prefix = re.sub(r'=.*', '', d)
    return d_prefix in history_deps


def _get_pip_deps(full_deps):
    for dep in full_deps:
        if isinstance(dep, dict) and 'pip' in dep:
            return dep


def _combine_env_data(env_data_full, env_data_hist):
    deps_full = env_data_full['dependencies']
    deps_hist = env_data_hist['dependencies']
    deps = [dep for dep in deps_full if _is_history_dep(dep, deps_hist)]

    pip_deps = _get_pip_deps(deps_full)

    env_data = {}
    env_data['channels'] = env_data_full['channels']
    env_data['dependencies'] = deps
    env_data['dependencies'].append(pip_deps)

    return env_data


def main(s_save=None):
    env_data_full = export_env()
    env_data_hist = export_env(history_only=True)
    env_data = _combine_env_data(env_data_full, env_data_hist)
    if s_save:
        with open(s_save, 'w') as y:
            yaml.dump(env_data, y)
    else:
        yaml.dump(env_data, sys.stdout)


if __name__ == '__main__':
    fire.Fire(main)

Tutorials教程

Save the codes above as conda_export_minimal.py .将上面的代码保存为conda_export_minimal.py

Specify CONDA_PATH with your conda's path (could be found by running command whereis conda ).使用CONDA_PATH的路径指定CONDA_PATH (可以通过运行命令whereis conda )。

Then run as follow:然后运行如下:

python conda_export_minimal.py --s_save=env.yml

A new file env.yml will appear in your current directory, which contains minimal dependencies about conda and pip.一个新文件env.yml将出现在您的当前目录中,其中包含有关 conda 和 pip 的最小依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM