Bazel rules to install Python dependencies with Poetry

Overview

rules_python_poetry

Bazel rules to install Python dependencies from a Poetry project. Works with native Python rules for Bazel.

Getting started

Add the following 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",
)

http_archive(
    name = "rules_python_poetry",
    url = "https://github.com/martinxsliu/rules_python_poetry/archive/v0.1.0.tar.gz",
    sha256 = "8f0abc58a8fcf75341b4615c6b7d9bb254119577629f45c2b1bb60f60f31b301",
    strip_prefix = "rules_python_poetry-0.1.0"
)

load("@rules_python_poetry//:defs.bzl", "poetry_install_toolchain", "poetry_install")

# Optional, if you want to use a specific version of Poetry (1.0.10 is the default).
poetry_install_toolchain(poetry_version = "1.1.4")

poetry_install(
    name = "my_deps",
    pyproject_toml = "//path/to:pyproject.toml",
    poetry_lock = "//path/to:poetry.lock",
    dev = True,  # Optional
)

Under the hood, poetry_install uses Poetry to export a requirements.txt file which is then passed to rule_python's pip_install repository rule. You can consume dependencies the same way as you would with pip_install, e.g.:

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

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

Poetry dependencies

Poetry allows you to specify dependencies from different types of sources that are not automatically fetched and installed by the poetry_install rule. You will have to manually declare these dependencies.

See tests/multi/app for examples.

Local directory dependency

A dependency on a local directory, for example if you have multiple projects within a monorepo that depend on each other.

[tool.poetry.dependencies]
foo = {path = "../libs/foo"}

If the local dependency has a py_library target, you can include it in the deps attribute.

py_library(
    name = "my_lib",
    srcs = ["my_lib.py"],
    deps = [
        "//path/to/libs:foo",
    ],
)

Local file dependency

A dependency on a local tarball, for example if you have vendored packages.

[tool.poetry.dependencies]
foo = {path = "../vendor/foo-1.2.3.tar.gz"}

There are some options available. The first is to extract the archive and vendor the extracted files. Then add a py_library that can be included as a deps, like the local directory dependency.

The second is to use the py_archive repository rule to declare the archive as an external repository in your WORKSPACE file, e.g.:

load("@rules_python_poetry//:defs.bzl", "py_archive")

py_archive(
    name = "foo",
    archive = "//path/to/vendor:foo-1.2.3.tar.gz",
    strip_prefix = "foo-1.2.3",
)

The py_archive rule defines a target named :py_library that can be referenced like so:

py_library(
    name = "my_lib",
    srcs = ["my_lib.py"],
    deps = [
        "@foo//:py_library",
    ],
)

URL dependency

A dependency on a remote archive.

[tool.poetry.dependencies]
foo = {url = "https://example.com/packages/foo-1.2.3.tar.gz"}

You can use the py_archive repository rule to declare the remote archive as an external repository in your WORKSPACE file, e.g.:

load("@rules_python_poetry//:defs.bzl", "py_archive")

py_archive(
    name = "foo",
    url = "https://example.com/packages/foo-1.2.3.tar.gz",
    sha256 = "...",
    strip_prefix = "foo-1.2.3",
)

The py_archive rule defines a target named :py_library that can be referenced like so:

py_library(
    name = "my_lib",
    srcs = ["my_lib.py"],
    deps = [
        "@foo//:py_library",
    ],
)

Git dependency

Git dependencies are not currently supported. You can work around this by using a URL dependency instead of a git key.

You might also like...
Python 3.9.4 Graphics and Compute Shader Framework and Primitives with no external module dependencies
Python 3.9.4 Graphics and Compute Shader Framework and Primitives with no external module dependencies

pyshader Python 3.9.4 Graphics and Compute Shader Framework and Primitives with no external module dependencies Fully programmable shader model (even

This library attempts to abstract the handling of Sigma rules in Python

This library attempts to abstract the handling of Sigma rules in Python. The rules are parsed using a schema defined with pydantic, and can be easily loaded from YAML files into a structured Python object.

Code and yara rules to detect and analyze Cobalt Strike

Cobalt Strike Resources This repository contains: analyze.py: a script to analyze a Cobalt Strike beacon (python analyze.py BEACON) extract.py; extrac

A Regex based linter tool that works for any language and works exclusively with custom linting rules.

renag Documentation Available Here Short for Regex (re) Nag (like "one who complains"). Now also PEGs (Parsing Expression Grammars) compatible with py

ChainJacking is a tool to find which of your Go lang direct GitHub dependencies is susceptible to ChainJacking attack.
ChainJacking is a tool to find which of your Go lang direct GitHub dependencies is susceptible to ChainJacking attack.

ChainJacking is a tool to find which of your Go lang direct GitHub dependencies is susceptible to ChainJacking attack.

A class to draw curves expressed as L-System production rules
A class to draw curves expressed as L-System production rules

A class to draw curves expressed as L-System production rules

An easy FASTA object handler, reader, writer and translator for small to medium size projects without dependencies.

miniFASTA An easy FASTA object handler, reader, writer and translator for small to medium size projects without dependencies. Installation Using pip /

An assistant to guess your pip dependencies from your code, without using a requirements file.

Pip Sala Bim is an assistant to guess your pip dependencies from your code, without using a requirements file. Pip Sala Bim will tell you which packag

Comments
  • Reference python3 instead of python in hashbang

    Reference python3 instead of python in hashbang

    This fixes a build error on Ubuntu 20.04:

    ERROR: /workspace/WORKSPACE:41:15: fetching poetry_export rule //external:py_deps_export: Traceback (most recent call last):
            File "/workspace/out/external/rules_python_poetry/internal/export.bzl", line 19, column 17, in _poetry_export_impl
                    fail("Poetry strip dependencies failed:\n%s\n%s" % (result.stdout, result.stderr))
    

    Not all distros have python pointing to python3 by default yet. It's possible to resolve the issue by installing python-is-python3 but IMO explicitly specifying the version is better since it avoids this build error without needing to modify the host machine.

    A future improvement could be to allow specifying a Bazel target which points to the Python target to use.

    opened by bduffany 0
  • How can I have a py_test target use this poetry toolchain?

    How can I have a py_test target use this poetry toolchain?

    my BUILD file is:

    load("@rules_python//python:defs.bzl", "py_test")
    
    py_test(
        name = "test_example",
        srcs = ["test_example.py"],
    )
    

    but when running test, it is still using the system python instead of the poetry venv, is this possible and if so can you update the README?

    opened by ruidc 0
Releases(v0.1.0)
  • v0.1.0(Nov 16, 2020)

Owner
Martin Liu
Martin Liu
My custom Fedora ostree build with sway/wayland.

Ramblurr's Sway Desktop This is an rpm-ostree based minimal Fedora developer desktop with the sway window manager and podman/toolbox for doing develop

Casey Link 1 Nov 28, 2021
The parser of a timetable of tennis matches for Flashscore website

FlashscoreParser The parser of a timetable of tennis matches for Flashscore website. The program collects the schedule of tennis matches for two days

Valendovsky 1 Jul 15, 2022
Python Service for MISP Feed Management

Python Service for MISP Feed Management This set of scripts is designed to offer better reliability and more control over the fetching of feeds into M

Chris 7 Aug 24, 2022
Python library for converting Python calculations into rendered latex.

Covert art by Joshua Hoiberg handcalcs: Python calculations in Jupyter, as though you wrote them by hand. handcalcs is a library to render Python calc

Connor Ferster 5.1k Jan 07, 2023
Open source tools to allow working with ESP devices in the browser

ESP Web Tools Allow flashing ESPHome or other ESP-based firmwares via the browser. Will automatically detect the board type and select a supported fir

ESPHome 195 Dec 31, 2022
MatroSka Mod Compiler for ts4scripts

MMC Current Version: 0.2 MatroSka Mod Compiler for .ts4script files Requirements Have Python 3.7 installed and set as default. Running from Source pip

MatroSka 1 Dec 13, 2021
This synchronizes my appearances with my calendar

Josh's Schedule Synchronizer Here's the "problem:" I use a Google Sheets spreadsheet to maintain all my public appearances.

Developer Advocacy 2 Oct 18, 2021
ToDoListAndroid - To-do list application created using Kivymd

ToDoListAndroid To-do list application created using Kivymd. Version 1.0.0 (1/Jan/2022). Planned to do next: -Add setting (theme selector, etc) -Add f

AghnatHs 1 Jan 01, 2022
This repo is related to Google Coding Challenge, given to Bright Network Internship Experience 2021.

BrightNetworkUK-GCC-2021 This repo is related to Google Coding Challenge, given to Bright Network Internship Experience 2021. Language used here is py

Dareer Ahmad Mufti 28 May 23, 2022
[draft] tools for schnetpack

schnetkit some tooling for schnetpack EXPERIMENTAL/IN DEVELOPMENT DO NOT USE This is an early draft of some infrastructure built around schnetpack. In

Marcel 1 Nov 08, 2021
An improved version of the common ˙pacman -S˙

BetterPacmanLook An improved version of the common pacman -S. Installation I know that this is probably one of the worst solutions and i will be worki

1 Nov 06, 2021
In this project we will implement AirBnB clone using console

AirBnB Clone In this project we will implement AirBnB clone using console. Usage The shell should work like this

Nandweza Allan 1 Feb 07, 2022
Курс про техническое совершенство для нетехнарей

Technical Excellence 101 Курс про техническое совершенство для нетехнарей. Этот курс представлят из себя серию воркшопов, при помощи которых можно объ

Anton Bevzuk 11 Nov 13, 2022
Control your gtps with gtps-tools!

Note Please give credit to me! Do not try to sell this app, because this app is 100% open source! Do not try to reupload and rename the creator app! S

Jesen N 6 Feb 16, 2022
pythonOS: An operating system kernel made in python and assembly

pythonOS An operating system kernel made in python and assembly Wait what? It uses a custom compiler called snek that implements a part of python3.9 (

Abbix 69 Dec 23, 2022
ChieriBot,词云API版,用于统计群友说过的怪话

wordCloud_API 词云API版,用于统计群友说过的怪话,基于wordCloud 消息储存在mysql数据库中.数据表结构见table.sql 为啥要做成API:这玩意太吃性能了,如果和Bot放在同一个服务器,可能会影响到bot的正常运行 你服务器性能够用的话就当我在放屁 依赖包 pip i

chinosk 7 Mar 20, 2022
Python most simple|stupid programming language (MSPL)

Most Simple|Stupid Programming language. (MSPL) Stack - Based programming language "written in Python" Features: Interpretate code (Run). Generate gra

Kirill Zhosul 14 Nov 03, 2022
🤖🤖 Jarvis is an virtual assistant which can some tasks easy for you like surfing on web opening an app and much more... 🤖🤖

Jarvis 🤖 🤖 Jarvis is an virtual assistant which can some tasks easy for you like surfing on web opening an app and much more... 🤖 🤖 Developer : su

1 Nov 08, 2021
A simple program to run through inputs for a 3n+1 problem

Author Tyler Windemuth Collatz_Conjecture A simple program to run through inputs for a 3n+1 problem Purpose: doesn't really have a purpose, did this t

0 Apr 22, 2022
Controller state monitor plugin for EVA ICS

eva-plugin-cmon Controller status monitor plugin for EVA ICS Monitors connected controllers status in SFA and pushes measurements into an external Inf

Altertech 1 Nov 06, 2021