• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1workspace(name = "rules_python_pip_install_example")
2
3local_repository(
4    name = "rules_python",
5    path = "../..",
6)
7
8load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
9
10py_repositories()
11
12python_register_toolchains(
13    name = "python39",
14    python_version = "3.9",
15)
16
17load("@python39//:defs.bzl", "interpreter")
18load("@rules_python//python:pip.bzl", "pip_install")
19
20pip_install(
21    # (Optional) You can provide extra parameters to pip.
22    # Here, make pip output verbose (this is usable with `quiet = False`).
23    #extra_pip_args = ["-v"],
24
25    # (Optional) You can exclude custom elements in the data section of the generated BUILD files for pip packages.
26    # Exclude directories with spaces in their names in this example (avoids build errors if there are such directories).
27    #pip_data_exclude = ["**/* */**"],
28
29    # (Optional) You can provide a python_interpreter (path) or a python_interpreter_target (a Bazel target, that
30    # acts as an executable). The latter can be anything that could be used as Python interpreter. E.g.:
31    # 1. Python interpreter that you compile in the build file (as above in @python_interpreter).
32    # 2. Pre-compiled python interpreter included with http_archive
33    # 3. Wrapper script, like in the autodetecting python toolchain.
34    #
35    # Here, we use the interpreter constant that resolves to the host interpreter from the default Python toolchain.
36    python_interpreter_target = interpreter,
37
38    # (Optional) You can set quiet to False if you want to see pip output.
39    #quiet = False,
40
41    # (Optional) You can set an environment in the pip process to control its
42    # behavior. Note that pip is run in "isolated" mode so no PIP_<VAR>_<NAME>
43    # style env vars are read, but env vars that control requests and urllib3
44    # can be passed.
45    #environment = {"HTTP_PROXY": "http://my.proxy.fun/"},
46
47    # Uses the default repository name "pip"
48    requirements = "//:requirements.txt",
49)
50
51load("@pip//:requirements.bzl", "install_deps")
52
53# Initialize repositories for all packages in requirements.txt.
54install_deps()
55
56# You could optionally use an in-build, compiled python interpreter as a toolchain,
57# and also use it to execute pip.
58#
59# Special logic for building python interpreter with OpenSSL from homebrew.
60# See https://devguide.python.org/setup/#macos-and-os-x
61#_py_configure = """
62#if [[ "$OSTYPE" == "darwin"* ]]; then
63#    ./configure --prefix=$(pwd)/bazel_install --with-openssl=$(brew --prefix openssl)
64#else
65#    ./configure --prefix=$(pwd)/bazel_install
66#fi
67#"""
68#
69# NOTE: you need to have the SSL headers installed to build with openssl support (and use HTTPS).
70# E.g. on Ubuntu: `sudo apt install libssl-dev`
71#http_archive(
72#    name = "python_interpreter",
73#    build_file_content = """
74#exports_files(["python_bin"])
75#filegroup(
76#    name = "files",
77#    srcs = glob(["bazel_install/**"], exclude = ["**/* *"]),
78#    visibility = ["//visibility:public"],
79#)
80#""",
81#    patch_cmds = [
82#        "mkdir $(pwd)/bazel_install",
83#        _py_configure,
84#        "make",
85#        "make install",
86#        "ln -s bazel_install/bin/python3 python_bin",
87#    ],
88#    sha256 = "dfab5ec723c218082fe3d5d7ae17ecbdebffa9a1aea4d64aa3a2ecdd2e795864",
89#    strip_prefix = "Python-3.8.3",
90#    urls = ["https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tar.xz"],
91#)
92
93# Optional:
94# Register the toolchain with the same python interpreter we used for pip in pip_install().
95#register_toolchains("//:my_py_toolchain")
96# End of in-build Python interpreter setup.
97