This document is an example demonstrating how to use Cyber RT Python API to write your own Python3 programs. Please make sure you have built Apollo successfully.
Save it as, say, path/to/my_demo.py
.
#!/usr/bin/env python3
import sys
from cyber.python.cyber_py3 import cyber
cyber.init()
if not cyber.ok():
print('Well, something went wrong.')
sys.exit(1)
# Do your job here.
cyber.shutdown()
Edit path/to/BUILD
file, add the followng section:
load("@rules_python//python:defs.bzl", "py_binary")
# blablahblah...
# Add your own section here
py_binary(
name = "my_demo",
srcs = ["my_demo.py"],
deps = [
"//cyber/python/cyber_py3:cyber",
],
)
Note: Like C++, Python code is also managed by Bazel starting from Apollo 6.0. Please refer to How to Build and Run Python Apps in Apollo for more on that.
Now you can run the following commands to build and run the demo program.
bazel build //path/to:my_demo
./bazel-bin/path/to/my_demo
Or simply run
bazel run //path/to:my_demo
Learn more Cyber RT Python examples under the examples and tests directory.