Registry / devops / pip-requirements-parser

pip-requirements-parser

JSON →
library32.0.1pypypi✓ verified 24d ago

pip-requirements-parser is a Python library designed for robustly parsing `pip` requirement files. It distinguishes itself by leveraging `pip`'s own internal parsing logic, ensuring high accuracy in handling the diverse and often complex `pip` requirement format. The library provides a stable, offline API for parsing, making it suitable for environments where network access is restricted or where precise `pip`-compatible parsing is critical. It is currently at version 32.0.1 and aims for a stable API for its users, despite the inherent complexities of `pip`'s internal requirement file format.

pip install pip-requirements-parser
INSTALL
IMPORT
SIG · PIP-REQUIREMENTS-P
P
pip-requirements-parser
devopspythonv32.0.1
Install
1.8s avg
Import
214ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v32.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.220s · 19.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.208s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
from pip_requirements_parser import parse
import requirements; requirements.parse
InvalidVersion
from packaging_legacy_version import InvalidVersion
LegacyVersion
from packaging_legacy_version import LegacyVersion

This quickstart demonstrates how to parse a string containing pip-style requirements using `requirements.parse()`. Each parsed requirement object provides attributes like `name`, `specs` (version specifiers), `vcs` (Version Control System information), and `url` for detailed analysis. The library also supports parsing directly from file-like objects.

import requirements req_string = """ django==3.2.1 requests>=2.25.1,<3.0.0 -e git+https://github.com/my/project.git#egg=myproject """ # Parse a string of requirements for req in requirements.parse(req_string): print(f"Name: {req.name}, Specs: {req.specs}, Vcs: {req.vcs}, Url: {req.url}") # You can also parse from a file-like object # with open('requirements.txt', 'r') as f: # for req in requirements.parse(f): # print(f"Name: {req.name}, Specs: {req.specs}")
Debug
Known issues
gotchaThe `pip` requirements file format is tightly coupled with `pip`'s internal implementation details and command-line options, making it notoriously difficult to parse correctly. While `pip-requirements-parser` aims to provide a stable API by incorporating `pip`'s own logic, users should be aware that the underlying `requirements.txt` format is not a stable public API and its full syntax can evolve with `pip` releases.
fix
Rely on `pip-requirements-parser`'s documented API. If issues arise with new `pip` requirement syntaxes, ensure you are using the latest version of `pip-requirements-parser` as it tracks `pip`'s internal changes.
affects: All versions
gotchaIt is generally discouraged to parse `requirements.txt` files directly to populate `install_requires` in `setup.py` or `pyproject.toml` for package distribution. Packaging metadata (e.g., in `pyproject.toml`'s `[project]` section) should use PEP 508 compliant specifiers for abstract dependencies, while `requirements.txt` is typically for concrete/pinned dependencies of applications.
fix
For package distribution, define dependencies in `pyproject.toml` (PEP 508). Use `pip-requirements-parser` only when you explicitly need to read and interpret the `requirements.txt` format itself, for example, in tools that analyze application environments.
affects: All versions
gotchaUnlike `pip` itself, which might perform network requests during its operations (e.g., resolving VCS requirements or fetching metadata), `pip-requirements-parser` is designed to work entirely offline. It parses the syntax without making external network calls.
fix
This is generally a feature for isolation and speed. If you require resolution of network-based requirements (e.g., installing packages from custom URLs or VCS), you will need to use `pip` directly or another tool that handles network operations.
affects: All versions
breakingThe `ModuleNotFoundError: No module named 'requirements'` indicates that the `pip-requirements-parser` library (which is typically imported as `requirements`) was not found in the Python environment. This means the library was not installed or its installation path was not accessible.
fix
Ensure `pip-requirements-parser` is installed in your Python environment by running `pip install pip-requirements-parser`. If you are using a virtual environment, make sure it is activated before installation.
affects: All versions
gotchaThe library `pip-requirements-parser` must be installed to be imported successfully. Encountering `ModuleNotFoundError: No module named 'requirements'` indicates that the package is either not installed or not accessible in the current Python environment.
fix
Ensure `pip-requirements-parser` is installed in your Python environment. You can typically install it using `pip install pip-requirements-parser`.
affects: All versions
Upgrade
Version history
32.0.1latest on PyPI · released Dec 21, 2022
Audit
Dependencies
packagingrequiredUsed for parsing and manipulating package version specifiers, as part of its internal logic derived from pip.
Agent activity
3 hits · last 30 days
node
2
Resources