requirements.rst (2342B)
1 Requirements 2 ============ 3 4 .. currentmodule:: packaging.requirements 5 6 Parse a given requirements line for specifying dependencies of a Python 7 project, using `PEP 508`_ which defines the scheme that has been implemented 8 by this module. 9 10 Usage 11 ----- 12 13 .. doctest:: 14 15 >>> from packaging.requirements import Requirement 16 >>> simple_req = Requirement("name") 17 >>> simple_req 18 <Requirement('name')> 19 >>> simple_req.name 20 'name' 21 >>> simple_req.url is None 22 True 23 >>> simple_req.extras 24 set() 25 >>> simple_req.specifier 26 <SpecifierSet('')> 27 >>> simple_req.marker is None 28 True 29 >>> # Requirements can be specified with extras, specifiers and markers 30 >>> req = Requirement('name[foo]>=2,<3; python_version>"2.0"') 31 >>> req.name 32 'name' 33 >>> req.extras 34 {'foo'} 35 >>> req.specifier 36 <SpecifierSet('<3,>=2')> 37 >>> req.marker 38 <Marker('python_version > "2.0"')> 39 >>> # Requirements can also be specified with a URL, but may not specify 40 >>> # a version. 41 >>> url_req = Requirement('name @ https://github.com/pypa ;os_name=="a"') 42 >>> url_req.name 43 'name' 44 >>> url_req.url 45 'https://github.com/pypa' 46 >>> url_req.extras 47 set() 48 >>> url_req.marker 49 <Marker('os_name == "a"')> 50 51 52 Reference 53 --------- 54 55 .. class:: Requirement(requirement) 56 57 This class abstracts handling the details of a requirement for a project. 58 Each requirement will be parsed according to PEP 508. 59 60 :param str requirement: The string representation of a requirement. 61 :raises InvalidRequirement: If the given ``requirement`` is not parseable, 62 then this exception will be raised. 63 64 .. attribute:: name 65 66 The name of the requirement. 67 68 .. attribute:: url 69 70 The URL, if any where to download the requirement from. Can be None. 71 72 .. attribute:: extras 73 74 A set of extras that the requirement specifies. 75 76 .. attribute:: specifier 77 78 A :class:`~.SpecifierSet` of the version specified by the requirement. 79 80 .. attribute:: marker 81 82 A :class:`~.Marker` of the marker for the requirement. Can be None. 83 84 .. exception:: InvalidRequirement 85 86 Raised when attempting to create a :class:`Requirement` with a string that 87 does not conform to PEP 508. 88 89 .. _`PEP 508`: https://www.python.org/dev/peps/pep-0508/