Requirements Parser
This small Python module provides utilities for parsing Pip `requirements.txt` files and individual requirement strings. It extracts package names, versions, operators, VCS information, and environment markers, making it easier to programmatically inspect and manipulate dependency specifications. The library is actively maintained, with version 0.13.0 recently released, and typically sees several releases per year to address bug fixes and add new parsing features.
Warnings
- breaking Support for Python 3.7 was officially dropped in version 0.8.0. Users on Python 3.7 must remain on `requirements-parser<0.8.0`.
- gotcha Version 0.11.0 replaced the deprecated `pkg_resources` module with `packaging` for internal parsing logic. While generally an improvement, users who might have relied on specific internal behaviors or properties exposed by `pkg_resources` in earlier versions could encounter subtle changes in parsing outcomes or object structures. Ensure thorough testing after upgrading.
- gotcha Parsing of editable installations (`-e .`) and VCS requirements with extras (`git+https://...#egg=package[extras]`) had fixes in versions 0.10.1 and 0.10.2 respectively. Older versions might incorrectly parse these common requirement types, leading to unexpected behavior or `ValueError` exceptions.
Install
-
pip install requirements-parser
Imports
- parse
from requirements.parser import parse
Quickstart
from requirements.parser import parse
requirement_string = "requests[security]==2.31.0; python_version<\"3.12\""
for req in parse(requirement_string):
print(f"Name: {req.name}")
print(f"Specs: {req.specs}")
print(f"Extras: {req.extras}")
print(f"URL: {req.url}")
print(f"VCS: {req.vcs}")
print(f"Hash: {req.hash}")
print(f"Environment Marker: {req.marker}")