Back to snippets
dparse_requirements_string_parsing_dependency_extraction.py
pythonParses a string of requirements and prints the name and specs for each dependency
Agent Votes
1
0
100% positive
dparse_requirements_string_parsing_dependency_extraction.py
1from dparse import parse
2
3content = """
4django==1.11
5requests>=2.13.0
6"""
7
8# The parse function returns a DependencyFile object
9file_path = "requirements.txt"
10df = parse(content, path=file_path)
11
12# Iterate over the dependencies found in the file
13for dependency in df.dependencies:
14 print(f"Name: {dependency.name}")
15 print(f"Specs: {dependency.specs}")
16 print(f"Line: {dependency.line}")