10

How can I (hermetically) include python as an (executable) input to my genrule?

Conceptually, I'm aware of the following approaches:

There also seem to be a couple methods for doing so:


Example:

I have a python library:

myproject
- setup.py
- mylibrary
  - __init__.py
  - myfunction.py

I'd like to produce a pip install-able distribution with Bazel so that I can do:

pip install <path/to/distribution>
python
>>> from mylibrary.myfunction import helloworld
>>> helloworld()

I added a (empty) WORKSPACE file at myproject/WORKSPACE and followed the example in this medium article:

# myproject/BUILD
genrule(
    name = "mylibrary_sdist",
    srcs = glob(["**/*.py"]),
    outs = ["mylibrary.tar.gz"],
    cmd = "python setup.py sdist && mv dist/mylibrary*.tar.gz $(location mylibrary.tar.gz)"
)

This works (ie. produces bazel-genfiles/mylibrary.tar.gz), but I'm shelling out to python.

How can I (hermetically) include python as an (executable) input to my genrule?

Pedro Cattori
  • 2,735
  • 1
  • 25
  • 43

1 Answers1

1

Unless I'm misunderstanding your question, this should just be a matter of passing the actual path to your target python executable. I would check the python executable into the third_party dir and instead of invoking your genrule using simply python I'd pass the relative path to that executable relative to WORKSPACE.

Venantius
  • 2,471
  • 2
  • 28
  • 36
  • what would I do if I want the bazel build to work cross-platform? I could include the compiled `python` interpreter in `third_party` as you state, but then it'd be compiled for a specific architecture. Is the best practice to include compiled versions for the architectures you want? Or I could include the source for python and have the compilation of the interpreter as part of my bazel build – Pedro Cattori Sep 17 '18 at 15:26
  • 2
    Well, my first instinct would be to say not to provide cross-platform support unless you really need it. But if you do need to, I'd probably start by checking in compiled executives for the relevant platforms - you can then leverage Bazel's [platform variables](https://docs.bazel.build/versions/master/platforms.html) to help Bazel reference the specific executable for the relevant host platform at build time. – Venantius Sep 17 '18 at 16:04
  • 1
    I don't think there's much upside in including the source for Python and compiling it as part of your build. If you think you're going to need to support a very large range of platforms, then that could be an option. But I'd start simple - pick a specific list of architectures / platforms you want to support, grab those executables, and check them in / link them. – Venantius Sep 17 '18 at 16:05