Support multiple conda environment in the same bazel project #51
Replies: 2 comments
-
Interesting findings. Since you have already emerged deeply into Bazel's way of working, your insight is valuable. Let me ask you directly: Do you think it is possible for |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, I have tried to find different ways to set a specific python_runtime per target, but it is impossible with the current official Python rules (https://bazel.build/reference/be/python). No, unless re-implementing all those rules from scratch at least. The only way seems to be to use the This means that if you have a big repo with two or more different Python targets relying on different conda environments (which is totally acceptable environment - that is the point of conda/virtualenv, after all, right?), you will have to have two bazel invocation. This also means you cannot have two different I guess the real point is that Bazel and Conda are trying to do the same thing - creating a hermetic environment. Bazel is not a wrapper for Conda. Its one-ring-to-rule-them-all nature would like to replace it. So if you want to compile targets with different conda environments, nature separates things. The same is for Bazel. All in all, using |
Beta Was this translation helpful? Give feedback.
-
I found myself in a not-really-bazelian use case recently, needing to support 2 or more conda environments in the same Bazel project tree.
At first, even if the great rules_conda supports the registration of multiple Python toolchains, it is impossible to specify which environment to use for a specific Python target.
However, rules_conda actually generates a BUILD file with a Python runtime.
That is if you define the following in the WORKSPACE:
You can actually query the external
conda_env
to be generated.So you can inspect each of those to look for a
python_runtime
If you need to know what files are a part of this python_runtime (and you feel adventurous), you can also add a
--output=build
.So once you know the Python runtime, if you have a Python target in app/BUILD, say something like:
You can then ensure a target is run with a specific Python runtime by doing the following:
you need to use
--incompatible_use_python_toolchains=false
if you want to use--python_top
flag to specify the runtime.With
--incompatible_use_python_toolchains=true
(that is the default),--python_top
cannot be specified, and by omitting it, bazel will use the first conda environment registered for all the python targets by default - in this example,coda_env1
Beta Was this translation helpful? Give feedback.
All reactions