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
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 /

Jules Kreuer 3 Jun 30, 2022
This repo will have a small amount of Chrome tools that can be used for DFIR, Hacking, Deception, whatever your heart desires.

Chrome-Tools Overview Welcome to the repo. This repo will have a small amount of Chrome tools that can be used for DFIR, Hacking, Deception, whatever

5 Jun 08, 2022
A performant state estimator for power system

A state estimator for power system. Turbocharged with sparse matrix support, JIT, SIMD and improved ordering.

9 Dec 12, 2022
Master Duel Card Translator Project

Master Duel Card Translator Project A tool for translating card effects in Yu-Gi-Oh! Master Duel. Quick Start (for Chinese version only) Download the

67 Dec 23, 2022
Percolation simulation using python

PythonPercolation Percolation simulation using python Exemple de percolation : Etude statistique sur le pourcentage de remplissage jusqu'à percolation

Tony Chouteau 1 Sep 08, 2022
A demo Piccolo app - a movie database!

PyMDb Welcome to the Python Movie Database! Built using Piccolo, Piccolo Admin, and FastAPI. Created for a presentation given at PyData Global 2021. R

11 Oct 16, 2022
Navigate to your directory of choice the proceed as follows

Installation 🚀 Navigate to your directory of choice the proceed as follows; 1 .Clone the git repo and create a virtual environment Depending on your

Ondiek Elijah Ochieng 2 Jan 31, 2022
Simple Denial of Service Program yang di bikin menggunakan bahasa pemograman Python,

Peringatan Tujuan kami share code Indo-DoS hanya untuk bertujuan edukasi / pembelajaran! Dilarang memperjual belikan source ini / memperjual-belikan s

SonLyte 8 Nov 07, 2021
Projeto de Jogo de dados em Python 3 onde é definido o lado a ser apostado e número de jogadas, pontuando os acertos e exibindo se ganhou ou perdeu.

Jogo de DadoX Projeto de script que simula um Jogo de dados em Python 3 onde é definido o lado a ser apostado (1, 2, 3, 4, 5 e 6) ou se vai ser um núm

Estênio Mariano 1 Jul 10, 2021
ChronoRace is a tool to accurately perform timed race conditions to circumvent application business logic.

ChronoRace is a tool to accurately perform timed race conditions to circumvent application business logic. I've found in my research that w

Tanner 64 Aug 04, 2022
Exam assignment for Laboratory of Bioinformatics 2

Exam assignment for Laboratory of Bioinformatics 2 (Alma Mater University of Bologna, Master in Bioinformatics)

2 Oct 22, 2022
For when you really need to rank things

Comparisonator For when you really need to rank things. Do you know that feeling when there's this urge deep within you that tells you to compare thin

Maciej Wilczyński 1 Nov 01, 2021
A simple interface to help lazy people like me to shutdown/reboot/sleep their computer remotely.

🦥 Lazy Helper ! A simple interface to help lazy people like me to shut down/reboot/sleep/lock/etc. their computer remotely. - USAGE If you're a lazy

MeHDI Rh 117 Nov 30, 2022
GitHub saver for stargazers, forks, repos

GitHub backup repositories Save your repos and list of stargazers & list of forks for them. Pure python3 and git with no dependencies to install. GitH

Alexander Kapitanov 23 Aug 21, 2022
PyGo custom language, New but similar language programming

New but similar language programming. Now we are capable to program in a very similar language to Python but at the same time get the efficiency of Go.

Fernando Perez 4 Nov 19, 2022
Statistics Calculator module for all types of Stats calculations.

Statistics-Calculator This Calculator user the formulas and methods to find the statistical values listed. Statistics Calculator module for all types

2 May 29, 2022
The CS Netlogo Helper is a small python script I made, to make computer science homework easier.

The CS Netlogo Helper is a small python script I made, to make computer science homework easier. This project is really ironic now that I think about it.

1 Jan 13, 2022
💻 Algo-Phantoms-Backend is an Application that provides pathways and quizzes along with a code editor to help you towards your DSA journey.📰🔥 This repository contains the REST APIs of the application.✨

Algo-Phantom-Backend 💻 Algo-Phantoms-Backend is an Application that provides pathways and quizzes along with a code editor to help you towards your D

Algo Phantoms 44 Nov 15, 2022
A scuffed remake of Kahoot... Made by Y9 and Y10 SHSB

A scuffed remake of Kahoot... Made by Y9 and Y10 SHSB

Tobiloba Kujore 3 Oct 28, 2022
The Python agent for Apache SkyWalking

SkyWalking Python Agent SkyWalking-Python: The Python Agent for Apache SkyWalking, which provides the native tracing abilities for Python project. Sky

The Apache Software Foundation 149 Dec 12, 2022