Skip to main content

Parse python requirements.txt files into setuptools extras

Project description

extreqs

Parse python requirements.txt files into setuptools extras.

Usage

extreqs looks for special comments (#extra:) in your requirements files. Note the lack of space after the #! Anything which follows that (until the end of line, or another #) is treated as a whitespace-separated list of extras. For example, #extra: dev test doc marks dependencies belonging to the dev, test, and doc extras.

If the #extra: comment is on the same line as (following) a dependency, then just that dependency belongs to that extra. If the #extra: comment is on a line on its own, all dependencies below it belong to that extra, until the next #extra: line.

For example:

# requirements.txt
dep1
dep2  #extra: extra1

#extra: extra2
dep3

#extra: extra3  # you can still have freeform comments!
dep4  #extra: extra4 extra5
dep5

would be parsed into

install_requires = ["dep1"]
extras_require = {
    "extra1": ["dep2"],
    "extra2": ["dep3"],
    "extra3": ["dep4", "dep5"],
    "extra4": ["dep4"],
    "extra5": ["dep4"],
}

Additionally, entire files can belong to a particular extra.

Note that python extras are not smart enough to deal with dependencies which belong only to combinations of extras, or negative extras: a dependency which belongs to multiple extras (given by the context of the file, block, or line) just belongs to multiple extras. This is a limitation of python packaging and cannot be addressed here.

In your setup.py:

#!/usr/bin/env python3
"""setup.py"""
from pathlib import Path

from extreqs import parse_requirements_files_dict
from setuptools import setup

here = Path(__file__).resolve().parent

req_kwargs = parse_requirements_files_dict(
    # files without an extra context are in *args
    here / "requirements.txt",
    # files with an extra context are in **kwargs
    optional=here / "requirements-optional.txt",
)

setup(
    name="my_package",
    ...
    **req_kwargs,
    ...
)

extreqs is an install-time dependency, and so must be added to your pyproject.toml:

# pyproject.toml
[build-system]
requires = ["setuptools", "extreqs"]
build-backend = "setuptools.build_meta"

Look out for dependency specifiers which are accepted by pip, but not by setuptools (e.g. editable install -e or references to other requirement files -r).

Notes

This package should only be used in certain circumstances, and may lead to bad habits if over-used. Requirements files are intended for specifying reproducible (i.e. hard version constraints), maximal environments for other developers, CI, and so on to be able to run all tests, features, lints etc.. Package dependencies are intended to specify the minimal dependencies with permissive version constraints for users to install the package for use.

This package is, therefore, more applicable to distributing applications (CLI, web backends, etc.) than it is libraries.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

extreqs-1.0.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

extreqs-1.0.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file extreqs-1.0.1.tar.gz.

File metadata

  • Download URL: extreqs-1.0.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for extreqs-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4544eb600e0bf9fbfa6e4cc4d2b7f4b718aec8d345fa0930f4c6254068871d0a
MD5 7b71d2401575844e2e6e8c64d0c5312d
BLAKE2b-256 26ac25cbd08da9a4c1158cd546c276f889deefc4291bb6b58a9f27bd627e8a73

See more details on using hashes here.

File details

Details for the file extreqs-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: extreqs-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for extreqs-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 29e6a30632edde9fdd0794696a17f46fae17905968aa5c4095c6ce8f8fc24bfd
MD5 c12e732adbacf34851ef2b4fdc5c3c50
BLAKE2b-256 6b7fdf549f516fd5a34a477ff1b2e6dc9dbdcb88e8b4fcfe30901d502cfef971

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page