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.
pip install dparseVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your Python interpreter to version 3.7 or newer, or pin `dparse<0.6.0` if unable to upgrade Python.
Migrate to using the main `dparse.parse` function, which now encapsulates the parsing logic for various file types and is the recommended entry point.
For projects relying heavily on `setup.py` for dependency definition, consider migrating to `pyproject.toml` or `setup.cfg` for more reliable and static dependency declaration. Always verify the parsed output for `setup.py` files.
Do not expect `dparse` to perform dependency resolution or installation. Use tools like `pip`, `Poetry`, or `Pipenv` for those tasks after parsing.
Install the package using pip: `pip install dparse`
Read the file content into a string and then pass it to the appropriate parsing method:
```python
from dparse.dependencies import Parser
parser = Parser()
with open("requirements.txt", "r") as f:
content = f.read()
dependencies = parser.parse_requirements(content)
# Or, for the generic parse method:
# dependencies = parser.parse(content)
```Import the `Parser` class and instantiate it to call its `parse` method:
```python
from dparse.dependencies import Parser
parser = Parser()
dependencies = parser.parse("numpy==1.20.0")
```Ensure you read the content of the file into a string before passing it to the parser:
```python
from dparse.dependencies import Parser
parser = Parser()
with open("requirements.txt", "r") as f:
content = f.read()
dependencies = parser.parse_requirements(content)
```