Low level API

Higher Level Parsing

Typically this is called via:

import requirements
requirements.parse('django>=1.5')
requirements.parser.parse(reqstr: str | TextIO) Iterator[Requirement][source]

Parse a requirements file into a list of Requirements

See: pip/req.py:parse_requirements()

Parameters:

reqstr – a string or file like object containing requirements

Returns:

a generator of Requirement objects

Lower Level Parsing

Under the hood, the Requirement class does most of the heavy lifting.

class requirements.requirement.Requirement(line: str)[source]

Represents a single requirement

Typically instances of this class are created with Requirement.parse. For local file requirements, there’s no verification that the file exists. This class attempts to be dict-like.

See: http://www.pip-installer.org/en/latest/logic.html

Members:

  • line - the actual requirement line being parsed

  • editable - a boolean whether this requirement is “editable”

  • local_file - a boolean whether this requirement is a local file/path

  • specifier - a boolean whether this requirement used a requirement specifier (eg. “django>=1.5” or “requirements”)

  • vcs - a string specifying the version control system

  • revision - a version control system specifier

  • name - the name of the requirement

  • uri - the URI if this requirement was specified by URI

  • subdirectory - the subdirectory fragment of the URI

  • path - the local path to the requirement

  • hash_name - the type of hashing algorithm indicated in the line

  • hash - the hash value indicated by the requirement line

  • extras - a list of extras for this requirement (eg. “mymodule[extra1, extra2]”)

  • specs - a list of specs for this requirement (eg. “mymodule>1.5,<1.6” => [(‘>’, ‘1.5’), (‘<’, ‘1.6’)])

classmethod parse(line: str) Requirement[source]

Parses a Requirement from a line of a requirement file.

Parameters:

line – a line of a requirement file

Returns:

a Requirement instance for the given line

Raises:

ValueError on an invalid requirement

classmethod parse_editable(line: str) Requirement[source]

Parses a Requirement from an “editable” requirement which is either a local project path or a VCS project URI.

See: pip/req.py:from_editable()

Parameters:

line – an “editable” requirement

Returns:

a Requirement instance for the given line

Raises:

ValueError on an invalid requirement

classmethod parse_line(line: str) Requirement[source]

Parses a Requirement from a non-editable requirement.

See: pip/req.py:from_line()

Parameters:

line – a “non-editable” requirement

Returns:

a Requirement instance for the given line

Raises:

ValueError on an invalid requirement

Misc Functions

requirements.parse(reqstr: str | TextIO) Iterator[Requirement][source]

Parse a requirements file into a list of Requirements

See: pip/req.py:parse_requirements()

Parameters:

reqstr – a string or file like object containing requirements

Returns:

a generator of Requirement objects