Search code examples
pythonbazel

How to (hermetically) include Python interpreter in Bazel to build Python library (sdist)


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?


Solution

  • 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.