3

So I am trying to combine using python and bazel together and I was wondering if there is a way to pass the values of a python script arguments while running that script through a bazel target.

So the idea is:

I have some main.py file that accepts args and executes some logic

#main.py
import argparse

class ClassA:
  def __init__(self, arg1):
    pass
  #incredible logic happens here :D 


def parse_arguments():
    parser = argparse.ArgumentParser(description="....")
    parser.add_argument(
        "--arg1",
        required=True)

if __name__ == "__main__":
    ARGS = parse_arguments()
    ClassA(ARGS.arg1) # will execute whatever has to happen and return something

usually, in order to run this script, I would just run something like:

python -m main.py --arg1=val1

but now I am interested in executing this script while having a bazel target for it, something like:

#BUILD 

py_binary( 
    name = "binary1",
    srcs = ["main.py"],
    main = "main.py",
    visibility = ["//public"],
)

Is there a way to run that script using somethin like: bazel run //path/to/py_binary --arg1=val1

If not what are the alternatives on running that python script while using this bazel target?

P.S. I am using Python 3.5+

1 Answer 1

4

You can do this by putting -- before the command line options you want to pass to the binary, like this:

bazel run //pkg:binary1 -- --arg1=val1

or

bazel run -- //pkg:binary1 --arg1=val1

See https://docs.bazel.build/versions/master/user-manual.html#run

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.