5

I have a python project which uses anaconda as its Python distribution. What's the best way to convert it to bazel. I.e. how to do the following?:

  • Install conda from the build script
  • Install required libraries into this conda distribution
  • Add dependencies on these libraries
Konstantin Solomatov
  • 10,252
  • 8
  • 58
  • 88

2 Answers2

1

I used bazel repository rules for this: https://docs.bazel.build/versions/master/skylark/repository_rules.html

Konstantin Solomatov
  • 10,252
  • 8
  • 58
  • 88
1

Here is an open-source set of rules for creating conda environments within a bazel build: https://github.com/spietras/rules_conda

The following example code is copied from the README of that github repository. It demonstrates how to create a conda environment from a @//:environment.yml file:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_conda",
    sha256 = "...",  # copy from release
    url = "...",  # copy from release
)

load("@rules_conda//:defs.bzl", "conda_create", "load_conda", "register_toolchain")

load_conda(
    conda_version = "4.10.3",  # version of conda to download, default is 4.10.3
    installer = "miniforge",  # which conda installer to download, either miniconda or miniforge, default is miniconda
    install_mamba = True,  # whether to install mamba, which is a faster drop-in replacement for conda, default is False
    mamba_version = "0.17.0",  # version of mamba to install, default is 0.17.0
    quiet = False,  # True if conda output should be hidden, default is True
    timeout = 600,  # how many seconds each execute action can take, default is 3600
)

conda_create(
    name = "env",  # name of the environment
    environment = "@//:environment.yml",  # label pointing to environment configuration file
    use_mamba = True,  # Whether to use mamba to create the conda environment. If this is True, install_mamba must also be True
    clean = False,  # True if conda cache should be cleaned (less space taken, but slower subsequent builds), default is False
    quiet = False,  # True if conda output should be hidden   True, default is True
    timeout = 600,  # how many seconds each execute action can take, default is 3600
)

register_toolchain(env = "env")
Jasha
  • 5,507
  • 2
  • 33
  • 44