Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

bazel

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

Conceptually, I'm aware of the following approaches:

  • include a python interpreter in the source repo at third_party/python
  • have Bazel fetch python ala rules-go

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

  • py_runtime
  • py_toolchain (doesn't seem to be available yet)
  • py_distribution_package (doesn't seem to be available yet)
  • genrules.tools
  • genrules.toolchains

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?

like image 408
Pedro Cattori Avatar asked Sep 07 '18 18:09

Pedro Cattori


People also ask

Does Bazel work with Python?

As we expected, Bazel is using Python version 3.8. 3 that we compiled from scratch and not Python 3.9.


1 Answers

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.

like image 78
Venantius Avatar answered Nov 15 '22 19:11

Venantius