This repo is a working example showing how to use Bazel with pex and pip.
This repo is meant to be educational. Therefore, please read the code.
The example shows:
- pulls dependencies using pip
- builds a single binary with pex
- reads a resource file
bazel build main.pex
./bazel-bin/main.pex
The output:
Current file's path: /usr/local/google/home/tanin/.pex/code/e7c476458b50c0339f6e515b84111b4dd9dee723/test.py
appdirs' path: /usr/local/google/home/tanin/.pex/install/appdirs-1.4.3-py2.py3-none-any.whl.88ca3acdb0294d36138be8eb8d73eb234d0cc1d3/appdirs-1.4.3-py2.py3-none-any.whl/appdirs.pyc
Test.json's content: TEST
Finished.
Notice that appdirs
comes from within .pex
.
Why do we choose pex over subpar?
Subpar doesn't support extracting resource files from the archive. If we have a more complex use case (e.g. executing a bash script), we can't use subpar.
Pex, on another hand, can set zip_safe=False
which extracts the archive before
executing the python code.
- The snippet on pip integration from @trivigy
- The pex integration from @benley