Skip to content

Instantly share code, notes, and snippets.

@gwerbin
Last active August 28, 2024 12:13
Show Gist options
  • Select an option

  • Save gwerbin/dab3cf5f8db07611c6e0aeec177916d8 to your computer and use it in GitHub Desktop.

Select an option

Save gwerbin/dab3cf5f8db07611c6e0aeec177916d8 to your computer and use it in GitHub Desktop.

Revisions

  1. gwerbin revised this gist Jun 7, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions conda_env_export.py
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@
    Exports only manually-installed dependencies, excluding build versions, but
    including Pip-installed dependencies.
    Lots of issues requesting this functionality in the Conda issue tracker, no
    sign of progress (as of March 2020).
    Lots of issues requesting this functionality in the Conda issue tracker,
    e.g. https://github.com/conda/conda/issues/9628
    TODO (?): support command-line flags -n and -p
  2. gwerbin revised this gist Jun 7, 2023. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions conda_env_export.py
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,28 @@
    sign of progress (as of March 2020).
    TODO (?): support command-line flags -n and -p
    MIT License:
    Copyright (c) 2021 @gwerbin
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    """
    import re
    import subprocess
  3. gwerbin revised this gist Jul 15, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions conda_env_export.py
    Original file line number Diff line number Diff line change
    @@ -29,8 +29,8 @@ def export_env(history_only=False, include_builds=False):
    cp = subprocess.run(cmd, stdout=subprocess.PIPE)
    try:
    cp.check_returncode()
    except Exception as e:
    raise e
    except:
    raise
    else:
    return yaml.safe_load(cp.stdout)

  4. gwerbin revised this gist Jun 28, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion conda_env_export.py
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@ def _get_pip_deps(full_deps):

    def _combine_env_data(env_data_full, env_data_hist):
    deps_full = env_data_full['dependencies']
    deps_hist = (env_data_hist['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)
  5. gwerbin created this gist Jan 8, 2021.
    79 changes: 79 additions & 0 deletions conda_env_export.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    """
    Export a Conda environment with --from-history, but also append
    Pip-installed dependencies
    Exports only manually-installed dependencies, excluding build versions, but
    including Pip-installed dependencies.
    Lots of issues requesting this functionality in the Conda issue tracker, no
    sign of progress (as of March 2020).
    TODO (?): support command-line flags -n and -p
    """
    import re
    import subprocess
    import sys

    import yaml


    def export_env(history_only=False, include_builds=False):
    """ Capture `conda env export` output """
    cmd = ['conda', 'env', 'export']
    if history_only:
    cmd.append('--from-history')
    if include_builds:
    raise ValueError('Cannot include build versions with "from history" mode')
    if not include_builds:
    cmd.append('--no-builds')
    cp = subprocess.run(cmd, stdout=subprocess.PIPE)
    try:
    cp.check_returncode()
    except Exception as e:
    raise e
    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():
    env_data_full = export_env()
    env_data_hist = export_env(history_only=True)
    env_data = _combine_env_data(env_data_full, env_data_hist)
    yaml.dump(env_data, sys.stdout)
    print('Warning: this output might contain packages installed from non-public sources, e.g. a Git repository. '
    'You should review and test the output to make sure it works with `conda env create -f`, '
    'and make changes as required.\n'
    'For example, `conda-env-export` itself is not currently uploaded to PyPI, and it must be removed from '
    'the output file, or else `conda create -f` will fail.', file=sys.stderr)


    if __name__ == '__main__':
    main()
    1 change: 1 addition & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    pyyaml