dparse - Python Dependency File Parser

0.6.4 · active · verified Thu Apr 09

dparse is a Python library designed to parse various Python dependency file formats such as `requirements.txt`, `Pipfile`, `Pipfile.lock`, `poetry.lock`, `setup.py`, `setup.cfg`, and `pyproject.toml`. It extracts dependency information, including names, versions, and extras, into a structured format. The current version is 0.6.4, with releases occurring on an irregular but active basis.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `dparse.parse` to extract dependencies from a string representing a `requirements.txt` file and a `Pipfile.lock`. The `filename` argument helps `dparse` identify the correct parsing strategy. The output shows the name and version specifier for each detected dependency.

from dparse import parse

requirements_content = """
flask>=2.0.0
requests==2.28.1; python_version < "3.9"
django
"""

dependencies = parse(requirements_content, filename='requirements.txt')

print(f"Parsed {len(dependencies.dependencies)} dependencies:")
for dep in dependencies.dependencies:
    print(f"- {dep.name}: {dep.specifier} (line {dep.line_number})")

# Example for a specific file type (e.g., Pipfile.lock)
pipfile_lock_content = """
{'_meta': {'hash': {'sha256': '...'}},
 'default': {
  'requests': {'hashes': [...], 'version': '==2.28.1'}}
}
"""
dep_from_lock = parse(pipfile_lock_content, filename='Pipfile.lock')
print(f"\nParsed {len(dep_from_lock.dependencies)} from Pipfile.lock:")
for dep in dep_from_lock.dependencies:
    print(f"- {dep.name}: {dep.specifier}")

view raw JSON →