1*60517a1eSAndroid Build Coastguard Worker# Getting started 2*60517a1eSAndroid Build Coastguard Worker 3*60517a1eSAndroid Build Coastguard WorkerThis doc is a simplified guide to help get started started quickly. It provides 4*60517a1eSAndroid Build Coastguard Workera simplified introduction to having a working Python program for both bzlmod 5*60517a1eSAndroid Build Coastguard Workerand the older way of using `WORKSPACE`. 6*60517a1eSAndroid Build Coastguard Worker 7*60517a1eSAndroid Build Coastguard WorkerIt assumes you have a `requirements.txt` file with your PyPI dependencies. 8*60517a1eSAndroid Build Coastguard Worker 9*60517a1eSAndroid Build Coastguard WorkerFor more details information about configuring `rules_python`, see: 10*60517a1eSAndroid Build Coastguard Worker* [Configuring the runtime](toolchains) 11*60517a1eSAndroid Build Coastguard Worker* [Configuring third party dependencies (pip/pypi)](pypi-dependencies) 12*60517a1eSAndroid Build Coastguard Worker* [API docs](api/index) 13*60517a1eSAndroid Build Coastguard Worker 14*60517a1eSAndroid Build Coastguard Worker## Using bzlmod 15*60517a1eSAndroid Build Coastguard Worker 16*60517a1eSAndroid Build Coastguard WorkerThe first step to using rules_python with bzlmod is to add the dependency to 17*60517a1eSAndroid Build Coastguard Workeryour MODULE.bazel file: 18*60517a1eSAndroid Build Coastguard Worker 19*60517a1eSAndroid Build Coastguard Worker```starlark 20*60517a1eSAndroid Build Coastguard Worker# Update the version "0.0.0" to the release found here: 21*60517a1eSAndroid Build Coastguard Worker# https://github.com/bazelbuild/rules_python/releases. 22*60517a1eSAndroid Build Coastguard Workerbazel_dep(name = "rules_python", version = "0.0.0") 23*60517a1eSAndroid Build Coastguard Worker 24*60517a1eSAndroid Build Coastguard Workerpip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") 25*60517a1eSAndroid Build Coastguard Workerpip.parse( 26*60517a1eSAndroid Build Coastguard Worker hub_name = "my_deps", 27*60517a1eSAndroid Build Coastguard Worker python_version = "3.11", 28*60517a1eSAndroid Build Coastguard Worker requirements_lock = "//:requirements.txt", 29*60517a1eSAndroid Build Coastguard Worker) 30*60517a1eSAndroid Build Coastguard Workeruse_repo(pip, "my_deps") 31*60517a1eSAndroid Build Coastguard Worker``` 32*60517a1eSAndroid Build Coastguard Worker 33*60517a1eSAndroid Build Coastguard Worker## Using a WORKSPACE file 34*60517a1eSAndroid Build Coastguard Worker 35*60517a1eSAndroid Build Coastguard WorkerUsing WORKSPACE is deprecated, but still supported, and a bit more involved than 36*60517a1eSAndroid Build Coastguard Workerusing Bzlmod. Here is a simplified setup to download the prebuilt runtimes. 37*60517a1eSAndroid Build Coastguard Worker 38*60517a1eSAndroid Build Coastguard Worker```starlark 39*60517a1eSAndroid Build Coastguard Workerload("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") 40*60517a1eSAndroid Build Coastguard Worker 41*60517a1eSAndroid Build Coastguard Worker 42*60517a1eSAndroid Build Coastguard Worker# Update the SHA and VERSION to the lastest version available here: 43*60517a1eSAndroid Build Coastguard Worker# https://github.com/bazelbuild/rules_python/releases. 44*60517a1eSAndroid Build Coastguard Worker 45*60517a1eSAndroid Build Coastguard WorkerSHA="84aec9e21cc56fbc7f1335035a71c850d1b9b5cc6ff497306f84cced9a769841" 46*60517a1eSAndroid Build Coastguard Worker 47*60517a1eSAndroid Build Coastguard WorkerVERSION="0.23.1" 48*60517a1eSAndroid Build Coastguard Worker 49*60517a1eSAndroid Build Coastguard Workerhttp_archive( 50*60517a1eSAndroid Build Coastguard Worker name = "rules_python", 51*60517a1eSAndroid Build Coastguard Worker sha256 = SHA, 52*60517a1eSAndroid Build Coastguard Worker strip_prefix = "rules_python-{}".format(VERSION), 53*60517a1eSAndroid Build Coastguard Worker url = "https://github.com/bazelbuild/rules_python/releases/download/{}/rules_python-{}.tar.gz".format(VERSION,VERSION), 54*60517a1eSAndroid Build Coastguard Worker) 55*60517a1eSAndroid Build Coastguard Worker 56*60517a1eSAndroid Build Coastguard Workerload("@rules_python//python:repositories.bzl", "py_repositories") 57*60517a1eSAndroid Build Coastguard Worker 58*60517a1eSAndroid Build Coastguard Workerpy_repositories() 59*60517a1eSAndroid Build Coastguard Worker 60*60517a1eSAndroid Build Coastguard Workerload("@rules_python//python:repositories.bzl", "python_register_toolchains") 61*60517a1eSAndroid Build Coastguard Worker 62*60517a1eSAndroid Build Coastguard Workerpython_register_toolchains( 63*60517a1eSAndroid Build Coastguard Worker name = "python_3_11", 64*60517a1eSAndroid Build Coastguard Worker # Available versions are listed in @rules_python//python:versions.bzl. 65*60517a1eSAndroid Build Coastguard Worker # We recommend using the same version your team is already standardized on. 66*60517a1eSAndroid Build Coastguard Worker python_version = "3.11", 67*60517a1eSAndroid Build Coastguard Worker) 68*60517a1eSAndroid Build Coastguard Worker 69*60517a1eSAndroid Build Coastguard Workerload("@python_3_11//:defs.bzl", "interpreter") 70*60517a1eSAndroid Build Coastguard Worker 71*60517a1eSAndroid Build Coastguard Workerload("@rules_python//python:pip.bzl", "pip_parse") 72*60517a1eSAndroid Build Coastguard Worker 73*60517a1eSAndroid Build Coastguard Workerpip_parse( 74*60517a1eSAndroid Build Coastguard Worker ... 75*60517a1eSAndroid Build Coastguard Worker python_interpreter_target = interpreter, 76*60517a1eSAndroid Build Coastguard Worker ... 77*60517a1eSAndroid Build Coastguard Worker) 78*60517a1eSAndroid Build Coastguard Worker``` 79*60517a1eSAndroid Build Coastguard Worker 80*60517a1eSAndroid Build Coastguard Worker## "Hello World" 81*60517a1eSAndroid Build Coastguard Worker 82*60517a1eSAndroid Build Coastguard WorkerOnce you've imported the rule set using either Bzlmod or WORKSPACE, you can then 83*60517a1eSAndroid Build Coastguard Workerload the core rules in your `BUILD` files with the following: 84*60517a1eSAndroid Build Coastguard Worker 85*60517a1eSAndroid Build Coastguard Worker```starlark 86*60517a1eSAndroid Build Coastguard Workerload("@rules_python//python:defs.bzl", "py_binary") 87*60517a1eSAndroid Build Coastguard Worker 88*60517a1eSAndroid Build Coastguard Workerpy_binary( 89*60517a1eSAndroid Build Coastguard Worker name = "main", 90*60517a1eSAndroid Build Coastguard Worker srcs = ["main.py"], 91*60517a1eSAndroid Build Coastguard Worker deps = [ 92*60517a1eSAndroid Build Coastguard Worker "@my_deps//foo", 93*60517a1eSAndroid Build Coastguard Worker "@my_deps//bar", 94*60517a1eSAndroid Build Coastguard Worker ] 95*60517a1eSAndroid Build Coastguard Worker) 96*60517a1eSAndroid Build Coastguard Worker``` 97