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
All kinds of programs are accepted here, raise a genuine PR, and claim a PR, Make 4 successful PR's and get the Stickers and T-Shirt from hacktoberfest 2021

this repository is excluded from hacktoberfest Hacktoberfest-2021 This repository aims to help code beginners with their first successful pull request

34 Sep 11, 2022
A Bot Which Can generate Random Account Based On Your Hits.

AccountGenBot This Bot Can Generate Account With Hits You Save (Randomly) Keyfeatures Join To Use Support Limit Account Generation Using Sql Customiza

DevsExpo 30 Oct 21, 2022
Rock 💎 Paper 📝 Scissors ✂️ Lizard 🦎 Spock 🖖

Rock 💎 Paper 📝 Scissors ✂️ Lizard 🦎 Spock 🖖 If you’ve seen The Big Bang Theory, you’ve heard of a game called “Rock, Paper, Scissors, Lizard, Spoc

AmirHossein Mohammadi 16 Jun 19, 2022
Aoc 2021 kedro playground with python

AOC 2021 Overview This is your new Kedro project, which was generated using Kedro 0.17.5. Take a look at the Kedro documentation to get started. Rules

1 Dec 20, 2021
Pseudometa's dotfiles

pseudometa's dotfiles Table of Contents Why this repository? How this Repository works Special Explanations Got an idea for an improvement? Contact Wh

pseudometa 23 Dec 27, 2022
Buffer Overflows

BOF Buffer Overflows 1. BOF tips Practice using mona.py Download vulnerable exe from Exploit DB.

Vinh Nguyá»…n 27 Dec 08, 2022
Tethered downgrade 64-bit iDevices vulnerable to checkm8

ra1nstorm Tethered downgrade 64-bit iDevices vulnerable to checkm8 Since the purpose of this tool is to tethered downgrade a device, after restoring p

mini_exploit 65 Nov 08, 2022
Lectures for Udemy - Complete Python Bootcamp Course

Complete-Python-Bootcamp Welcome to the Repository for the Complete Python Bootcamp! This is the Repository for the Udemy course - "Complete Python Bo

Marci 2k Dec 28, 2022
Import Apex legends mprt files exported from Legion

Apex-mprt-importer-for-Blender Import Apex legends mprt files exported from Legion. REQUIRES CAST IMPORTER Usage: Use a VPK extracter to extract the m

15 Dec 18, 2022
Python / C++ based particle reaction-diffusion simulator

ReaDDy (Reaction Diffusion Dynamics) is an open source particle based reaction-diffusion simulator that can be configured and run via Python. Currentl

ReaDDy 46 Dec 09, 2022
A brainfuck-based game oriented language written in python.

GF.py STILL WIP Gamefuck.py is a programming language based off brainfuck. It is oriented towards game development, and as such has many commands spec

Xenon 1 Feb 23, 2022
Final project for ENGG 5402 Advanced Robotics in CUHK

Final project Final project Update Foundations Ubuntu virtual machine Ubuntu How to use Github to keep tracking the change of code version? Docker Set

Junjia Liu 8 Aug 01, 2022
Grade 8 Version of Space Invaders

Space-Invaders Grade 8 Version of Space Invaders Compatability This program is Python 3 Compatable, and not Python 2 Compatable because i haven't test

Space64 0 Feb 16, 2022
Empresas do Brasil (CNPJs)

Biblioteca em Python que coleta informações cadastrais de empresas do Brasil (CNPJ) obtidas de fontes oficiais (Receita Federal) e exporta para um formato legível por humanos (CSV ou JSON).

BR-API: Democratizando dados do Brasil. 8 Aug 17, 2022
YBlade - Import QBlade blades into Fusion 360

YBlade - Import QBlade blades into Fusion 360 Simple script for Fusion 360 that takes QBlade blade description and constructs the blade: Usage First,

Jan Mrázek 37 Sep 25, 2022
A example project's description is a high-level overview of why you’re doing a project.

A example project's description is a high-level overview of why you’re doing a project.

Nikita Matyukhin 12 Mar 23, 2022
Modern robots.txt Parser for Python

Robots Exclusion Protocol Parser for Python Robots.txt parsing in Python. Goals Fetching -- helper utilities for fetching and parsing robots.txts, inc

Moz 176 Dec 16, 2022
It was created to conveniently respond to events such as donation, follow, and hosting using the Alert Box provided by twip to streamers

This library is not an official library of twip. It was created to conveniently respond to events such as donation, follow, and hosting using the Alert Box provided by twip to streamers.

junah201 8 Nov 19, 2022
p5 is a Python package based on the core ideas of Processing.

p5 p5 is a Python library that provides high level drawing functionality to help you quickly create simulations and interactive art using Python. It c

p5py 645 Jan 04, 2023
Tutorial on Tempo, Beat and Downbeat estimation

Tempo, Beat and Downbeat Estimation By Matthew E. P. Davies, Sebastian Böck and Magdalena Fuentes Resources and Jupyter Book for the ISMIR 2021 tutori

49 Nov 06, 2022