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