19

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'?

All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111

1 Answers1

24

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Can you give an example with this dependency? `"argon2-cffi (>=16.1.0); extra == 'argon2'"` I have came across something like `somepackage[bcrypt]` when installing `celery` but cant relate to the type dependency in the question. – All Іѕ Vаиітy Sep 24 '18 at 08:10
  • @Marty: `somepackage[argon2]` would install the package with that optional extra. – Martijn Pieters Sep 24 '18 at 08:10
  • what is somepackage anyway when coming to `"argon2-cffi (>=16.1.0); extra == 'argon2'"`? is it `argon2-cffi[argon2]>=16.1.0`? – All Іѕ Vаиітy Sep 24 '18 at 08:12
  • @Marty: no, it's the name of the package you found this specification in. – Martijn Pieters Sep 24 '18 at 08:12
  • so if the package is django, how does it relate? `Django[argon2]`? so this `argon2-cffi (>=16.1.0)` prefered over `Django[argon2]` in the statement or vice versa? – All Іѕ Vаиітy Sep 24 '18 at 08:13
  • @Marty: if you use `pip install Django[argon2]` then `pip` adds `argon2-cffi >=16.1.0` to the dependencies to install. Use `Django[argon2]`, always, because that way you make the Django2 package responsible for specifying the right packages and versions. – Martijn Pieters Sep 24 '18 at 08:17
  • so this is something to say 'install `argon2`' version that `Django` depends on? – All Іѕ Vаиітy Sep 24 '18 at 08:19
  • @Marty: yes, it means Django will use argon2-cffi if you installed that package, and then Django can make use of that, see https://docs.djangoproject.com/en/2.1/topics/auth/passwords/#using-argon2-with-django – Martijn Pieters Sep 24 '18 at 09:11