dparse2

raw JSON →
0.7.0 verified Fri May 01 auth: no python

A robust fork of the original dparse library with up-to-date dependency file parsing (pip requirements, Pipfile, conda env yml, poetry, setup.cfg). Version 0.7.0 supports Python 3.6+. Maintained by nexB.

pip install dparse2
error ModuleNotFoundError: No module named 'dparse'
cause Package renamed to dparse2; old import fails.
fix
Run 'pip install dparse2' and change imports to 'import dparse2'.
error AttributeError: module 'dparse2' has no attribute 'parse'
cause Wrong import path; parse is a function, not a module attribute when using 'import dparse2'.
fix
Use 'from dparse2 import parse' or 'dparse2.parse(...)'.
error TypeError: parse() missing 1 required positional argument: 'file_type'
cause parse() requires a file type class as second argument.
fix
Provide a file type, e.g., 'from dparse2.filetypes import RequirementsFile' and call 'parse(content, RequirementsFile)'.
breaking Package renamed from dparse to dparse2. All imports must use dparse2.
fix Replace 'import dparse' with 'import dparse2'.
breaking Python 2.7 support dropped after 0.5.0.1. Versions 0.6+ require Python 3.6+.
fix Upgrade to Python 3.6+ or use dparse2==0.5.0.1.
gotcha parse() returns a ParsedFile object; dependencies can be empty if file is malformed. Always check dependencies before iteration.
fix Use 'if parsed.dependencies: for dep in parsed.dependencies: ...'
gotcha RequirementsFile parser expects base directory (default: '.'). Relative paths in requirements.txt are resolved against the base dir.
fix Pass base_dir parameter to parse() to control relative path resolution.

Parses a requirements.txt file and prints each dependency name and version specifier.

from dparse2 import parse
from dparse2.filetypes import RequirementsFile

with open('requirements.txt') as f:
    content = f.read()
parsed = parse(content, RequirementsFile)
for dep in parsed.dependencies:
    print(dep.name, dep.specs)