All Projects → bazelbuild → Rules_python

bazelbuild / Rules_python

Licence: apache-2.0
Experimental Bazel Python Rules

Programming Languages

python
139335 projects - #7 most used programming language
python3
1442 projects
python2
120 projects
python27
39 projects

Projects that are alternatives of or similar to Rules python

rules poetry
Bazel rules that use Poetry for Python package management
Stars: ✭ 40 (-82.83%)
Mutual labels:  bazel, bazel-rules, pip
Dbx build tools
Dropbox's Bazel rules and tools
Stars: ✭ 119 (-48.93%)
Mutual labels:  bazel, bazel-rules
Bazel Tools
Reusable bits for Bazel
Stars: ✭ 109 (-53.22%)
Mutual labels:  bazel, bazel-rules
Rules apple
Bazel rules to build apps for Apple platforms.
Stars: ✭ 217 (-6.87%)
Mutual labels:  bazel, bazel-rules
Rules protobuf
Bazel rules for building protocol buffers and gRPC services (java, c++, go, ...)
Stars: ✭ 206 (-11.59%)
Mutual labels:  bazel, bazel-rules
Bazel Linting System
🌿💚 Experimental system for registering, configuring, and invoking source-code linters in Bazel.
Stars: ✭ 63 (-72.96%)
Mutual labels:  bazel, bazel-rules
Rules apple line
LINE's Apple rules for Bazel
Stars: ✭ 151 (-35.19%)
Mutual labels:  bazel, bazel-rules
Platforms
Constraint values for specifying platforms and toolchains
Stars: ✭ 34 (-85.41%)
Mutual labels:  bazel, bazel-rules
Rules swift
Bazel rules to build Swift on Apple and Linux platforms
Stars: ✭ 151 (-35.19%)
Mutual labels:  bazel, bazel-rules
Bazel Skylib
Common useful functions and rules for Bazel
Stars: ✭ 153 (-34.33%)
Mutual labels:  bazel, bazel-rules
Rules haskell
Haskell rules for Bazel.
Stars: ✭ 196 (-15.88%)
Mutual labels:  bazel, bazel-rules
Rules python external
Bazel rules to resolve and fetch artifacts transitively from the Python Package Index (PyPI)
Stars: ✭ 60 (-74.25%)
Mutual labels:  bazel, pip
Pigar
☕️ A fantastic tool to generate requirements.txt for your Python project, and more than that. (IT IS NOT A PACKAGE MANAGEMENT TOOL)
Stars: ✭ 1,068 (+358.37%)
Mutual labels:  pypi, pip
Rules nixpkgs
Rules for importing Nixpkgs packages into Bazel.
Stars: ✭ 88 (-62.23%)
Mutual labels:  bazel, bazel-rules
Rules grafana
Bazel rules for building Grafana dashboards
Stars: ✭ 46 (-80.26%)
Mutual labels:  bazel, bazel-rules
Dephell
📦 🔥 Python project management. Manage packages: convert between formats, lock, install, resolve, isolate, test, build graph, show outdated, audit. Manage venvs, build package, bump version.
Stars: ✭ 1,730 (+642.49%)
Mutual labels:  pypi, pip
Fades
fades is a system that automatically handles the virtualenvs in the cases normally found when writing scripts and simple programs, and even helps to administer big projects.
Stars: ✭ 182 (-21.89%)
Mutual labels:  pypi, pip
Rules codeowners
Bazel rules for generating CODEOWNERS from a workspace.
Stars: ✭ 31 (-86.7%)
Mutual labels:  bazel, bazel-rules
Python Pixabay
Python 3 Pixabay's API wrapper.
Stars: ✭ 32 (-86.27%)
Mutual labels:  pypi, pip
Audioowl
Fast and simple music and audio analysis using RNN in Python 🕵️‍♀️ 🥁
Stars: ✭ 151 (-35.19%)
Mutual labels:  pypi, pip

Python Rules for Bazel

  • Postsubmit Build status
  • Postsubmit + Current Bazel Incompatible Flags Build status

Recent updates

  • 2020-10-15: Release 0.1.0 was published, upstreaming the pip_install rule functionality from github.com/dillon-giacoppo/rules_python_external to address a number of long-standing issues with pip_import (eg. #96, #71, #102). Note that this is a backwards-incompatible release on account of the removal of pip_import from @rules_python//python:pip.bzl.

  • 2019-11-15: Added support for pip3_import (and more generally, a python_interpreter attribute to pip_import). The canonical naming for wheel repositories has changed to accomodate loading wheels for both pip_import and pip3_import in the same build. To avoid breakage, please use requirement() instead of depending directly on wheel repo labels.

  • 2019-07-26: The canonical name of this repo has been changed from @io_bazel_rules_python to just @rules_python, in accordance with convention. Please update your WORKSPACE file and labels that reference this repo accordingly.

Overview

This repository is the home of the core Python rules -- py_library, py_binary, py_test, and related symbols that provide the basis for Python support in Bazel. It also contains packaging rules for integrating with PyPI (pip). Documentation lives in the docs/ directory and in the Bazel Build Encyclopedia.

Currently the core rules are bundled with Bazel itself, and the symbols in this repository are simple aliases. However, in the future the rules will be migrated to Starlark and debundled from Bazel. Therefore, the future-proof way to depend on Python rules is via this repository. SeeMigrating from the Bundled Rules below.

The core rules are stable. Their implementation in Bazel is subject to Bazel's backward compatibility policy. Once they are fully migrated to rules_python, they may evolve at a different rate, but this repository will still follow semantic versioning.

The packaging rules (pip_install, etc.) are less stable. We may make breaking changes as they evolve. There are no guarantees for rules underneath the experimental/ directory.

This repository is maintained by the Bazel community. Neither Google, nor the Bazel team, provides support for the code. However, this repository is part of the test suite used to vet new Bazel releases. See the How to contribute page for information on our development workflow.

Getting started

To import rules_python in your project, you first need to add it to your WORKSPACE file:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "rules_python",
    url = "https://github.com/bazelbuild/rules_python/releases/download/0.1.0/rules_python-0.1.0.tar.gz",
    sha256 = "b6d46438523a3ec0f3cead544190ee13223a52f6a6765a29eae7b7cc24cc83a0",
)

To depend on a particular unreleased version (not recommended), you can do:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

rules_python_version = "c8c79aae9aa1b61d199ad03d5fe06338febd0774" # Latest @ 2020-10-15

http_archive(
    name = "rules_python",
    sha256 = "5be9610a959772697f57ec66bb58c8132970686ed7fb0f1cf81b22ddf12f5368",
    strip_prefix = "rules_python-{}".format(rules_python_version),
    url = "https://github.com/bazelbuild/rules_python/archive/{}.zip".format(rules_python_version),
)

Once you've imported the rule set into your WORKSPACE using any of these methods, you can then load the core rules in your BUILD files with:

load("@rules_python//python:defs.bzl", "py_binary")

py_binary(
  name = "main",
  srcs = ["main.py"],
)

Using the packaging rules

The packaging rules create two kinds of repositories: A central repo that holds downloaded wheel files, and individual repos for each wheel's extracted contents. Users only need to interact with the central repo; the wheel repos are essentially an implementation detail. The central repo provides a WORKSPACE macro to create the wheel repos, as well as a function to call in BUILD files to translate a pip package name into the label of a py_library target in the appropriate wheel repo.

Importing pip dependencies

To add pip dependencies to your WORKSPACE is you load the pip_install function, and call it to create the individual wheel repos.

load("@rules_python//python:pip.bzl", "pip_install")

# Create a central repo that knows about the dependencies needed for
# requirements.txt.
pip_install(
   name = "my_deps",
   requirements = "//path/to:requirements.txt",
)

Note that since pip is executed at WORKSPACE-evaluation time, Bazel has no information about the Python toolchain and cannot enforce that the interpreter used to invoke pip matches the interpreter used to run py_binary targets. By default, pip_install uses the system command "python3". This can be overridden by passing the python_interpreter attribute or python_interpreter_target attribute to pip_install.

You can have multiple pip_installs in the same workspace, e.g. for Python 2 and Python 3. This will create multiple central repos that have no relation to one another, and may result in downloading the same wheels multiple times.

As with any repository rule, if you would like to ensure that pip_install is re-executed in order to pick up a non-hermetic change to your environment (e.g., updating your system python interpreter), you can completely flush out your repo cache with bazel clean --expunge.

Importing pip dependencies with pip_import (legacy)

The deprecated pip_import can still be used if needed.

load("@rules_python//python/legacy_pip_import:pip.bzl", "pip_import", "pip_repositories")

# Create a central repo that knows about the dependencies needed for requirements.txt.
pip_import(   # or pip3_import
   name = "my_deps",
   requirements = "//path/to:requirements.txt",
)

# Load the central repo's install function from its `//:requirements.bzl` file, and call it.
load("@my_deps//:requirements.bzl", "pip_install")
pip_install()

An example can be found in `examples/legacy_pip_import.

Consuming pip dependencies

Each extracted wheel repo contains a py_library target representing the wheel's contents. Rather than depend on this target's label directly -- which would require hardcoding the wheel repo's mangled name into your BUILD files -- you should instead use the requirement() function defined in the central repo's //:requirements.bzl file. This function maps a pip package name to a label. ("Extras" can be referenced using the pkg[extra] syntax.)

load("@my_deps//:requirements.bzl", "requirement")

py_library(
    name = "mylib",
    srcs = ["mylib.py"],
    deps = [
        ":myotherlib",
        requirement("some_pip_dep"),
        requirement("another_pip_dep[some_extra]"),
    ]
)

For reference, the wheel repos are canonically named following the pattern: @{central_repo_name}_pypi__{distribution}_{version}. Characters in the distribution and version that are illegal in Bazel label names (e.g. -, .) are replaced with _. While this naming pattern doesn't change often, it is not guaranted to remain stable, so use of the requirement() function is recommended.

Consuming Wheel Dists Directly

If you need to depend on the wheel dists themselves, for instance to pass them to some other packaging tool, you can get a handle to them with the whl_requirement macro. For example:

filegroup(	
    name = "whl_files",	
    data = [	
        whl_requirement("boto3"),	
    ]	
)

Migrating from the bundled rules

The core rules are currently available in Bazel as built-in symbols, but this form is deprecated. Instead, you should depend on rules_python in your WORKSPACE file and load the Python rules from @rules_python//python:defs.bzl.

A buildifier fix is available to automatically migrate BUILD and .bzl files to add the appropriate load() statements and rewrite uses of native.py_*.

# Also consider using the -r flag to modify an entire workspace.
buildifier --lint=fix --warnings=native-py <files>

Currently the WORKSPACE file needs to be updated manually as per Getting started above.

Note that Starlark-defined bundled symbols underneath @bazel_tools//tools/python are also deprecated. These are not yet rewritten by buildifier.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].