dparse - Python Dependency File Parser
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
- breaking Python 3.6 support was dropped in `dparse` version 0.6.0. Users on Python 3.6 or older must upgrade their Python environment to at least 3.7 to use versions 0.6.0 and later.
- breaking The `dparse.pyup_parser` module was removed in version 0.4.0. Direct imports from this module will fail.
- gotcha `dparse` provides heuristic parsing for `setup.py` files. Due to the dynamic nature of `setup.py`, complex or non-standard setups might not be fully or accurately parsed.
- gotcha `dparse` is solely a *parser* of dependency files. It does not resolve dependencies, install packages, or interact with package indexes. Its output is a structured representation of the dependencies as declared in the input file.
Install
-
pip install dparse
Imports
- parse
from dparse import parse
Quickstart
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}")