blob: 45d1962ad8d84d737964060a950838fa60c9862b [file] [log] [blame] [view]
Richard Levasseur327b4e32023-10-19 11:03:01 -07001# Getting started
2
Richard Levasseur1f176372024-04-23 15:36:56 -07003This doc is a simplified guide to help get started started quickly. It provides
4a simplified introduction to having a working Python program for both bzlmod
5and the older way of using `WORKSPACE`.
Richard Levasseur327b4e32023-10-19 11:03:01 -07006
Richard Levasseur1f176372024-04-23 15:36:56 -07007It assumes you have a `requirements.txt` file with your PyPI dependencies.
8
9For 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 Levasseur327b4e32023-10-19 11:03:01 -070013
14## Using bzlmod
15
Richard Levasseur327b4e32023-10-19 11:03:01 -070016The first step to using rules_python with bzlmod is to add the dependency to
17your 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.
22bazel_dep(name = "rules_python", version = "0.0.0")
Richard Levasseur327b4e32023-10-19 11:03:01 -070023
Richard Levasseur1f176372024-04-23 15:36:56 -070024pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
25pip.parse(
26 hub_name = "my_deps",
Richard Levasseur327b4e32023-10-19 11:03:01 -070027 python_version = "3.11",
Richard Levasseur1f176372024-04-23 15:36:56 -070028 requirements_lock = "//:requirements.txt",
Richard Levasseur327b4e32023-10-19 11:03:01 -070029)
Richard Levasseur1f176372024-04-23 15:36:56 -070030use_repo(pip, "my_deps")
Richard Levasseur327b4e32023-10-19 11:03:01 -070031```
32
Richard Levasseur327b4e32023-10-19 11:03:01 -070033## Using a WORKSPACE file
34
Richard Levasseur1f176372024-04-23 15:36:56 -070035Using WORKSPACE is deprecated, but still supported, and a bit more involved than
36using Bzlmod. Here is a simplified setup to download the prebuilt runtimes.
Richard Levasseur327b4e32023-10-19 11:03:01 -070037
38```starlark
39load("@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
45SHA="84aec9e21cc56fbc7f1335035a71c850d1b9b5cc6ff497306f84cced9a769841"
46
47VERSION="0.23.1"
48
49http_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
56load("@rules_python//python:repositories.bzl", "py_repositories")
57
58py_repositories()
Richard Levasseur327b4e32023-10-19 11:03:01 -070059
Richard Levasseur327b4e32023-10-19 11:03:01 -070060load("@rules_python//python:repositories.bzl", "python_register_toolchains")
61
62python_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
69load("@python_3_11//:defs.bzl", "interpreter")
70
71load("@rules_python//python:pip.bzl", "pip_parse")
72
73pip_parse(
74 ...
75 python_interpreter_target = interpreter,
76 ...
77)
78```
79
Richard Levasseur327b4e32023-10-19 11:03:01 -070080## "Hello World"
81
Richard Levasseur1f176372024-04-23 15:36:56 -070082Once you've imported the rule set using either Bzlmod or WORKSPACE, you can then
83load the core rules in your `BUILD` files with the following:
Richard Levasseur327b4e32023-10-19 11:03:01 -070084
85```starlark
86load("@rules_python//python:defs.bzl", "py_binary")
87
88py_binary(
89 name = "main",
90 srcs = ["main.py"],
Richard Levasseur1f176372024-04-23 15:36:56 -070091 deps = [
92 "@my_deps//foo",
93 "@my_deps//bar",
94 ]
Richard Levasseur327b4e32023-10-19 11:03:01 -070095)
96```