Search code examples
pythonpippypipython-packaging

What is 'extra' in pypi dependency?


In requires_dist section of a package's json response from pypi, it is given:

requires_dist : [
    "bcrypt; extra == 'bcrypt'",
    "argon2-cffi (>=16.1.0); extra == 'argon2'"
]

can anyone make it clear the second statement of each dependency, extra == 'bcrypt' and extra == 'argon2'?


Solution

  • Extras are dependencies you can install in addition to the regular dependencies, if you ask for them explicitly. See them as optional features.

    You can install these with the name after the ==, with the name of the package. For example, if you install somepackage and want to add the bcrypt optional feature, use:

    pip install somepackage[bcrypt]
    

    or

    pip install somepackage[argon2]
    

    or, to include both optional extras, separate the names with commas:

    pip install somepackage[bcrypt,argon2]
    

    although using somepackage[...] multiple times also works as pip is smart enough to know that the main package is already installed.

    pip (or whatever other package install tool) maps names listed in <packagename>[<extras_name>(,...)] to those entries in the requires_dict that use the <dependency_spec>; extra == '<extras_name>' format, adding on the dependency_specs to the list of things to install.

    See Installing Setuptools "Extras" in the Installing Packages section of the Python Packaging User Guide.

    It is up to the installed package itself to detect if all the dependencies for optional extra features are installed. A common pattern is to use try...except ImportError: guards to test for such extra dependencies being available.