The Python Oracle

What do square brackets mean in pip install?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Ominous Technology Looping

--

Chapters
00:00 Question
00:20 Accepted answer (Score 148)
01:12 Answer 2 (Score 113)
02:43 Answer 3 (Score 25)
03:14 Answer 4 (Score 19)
03:36 Thank you

--

Full question
https://stackoverflow.com/questions/4677...

Answer 1 links:
[script]: https://github.com/apache/airflow/blob/m...
[setup.py]: https://github.com/apache/airflow/blob/m...
[image]: https://i.stack.imgur.com/OI86F.png

Answer 2 links:
https://setuptools.readthedocs.io/en/lat...

Answer 3 links:
[pip manual]: https://pip.pypa.io/en/stable/reference/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pip #dependencymanagement

#avk47



ACCEPTED ANSWER

Score 191


The syntax that you are using is:

pip install "project[extra]"

In your case, you are installing the splinter package which has the added support for django.

pip install splinter django would install two packages named splinter and django.

pip install splinter[django], on the other hand, installs splinter, but it also installs optional dependencies defined by splinter using the keyword in the brackets. In this case, as of 2024-05-15 it's Django, lxml and cssselect.

Note that the keyword in brackets has nothing to do with the django package itself, but is just a string defined by the splinter package for a particular set of dependencies that also get installed. How the argument django is interpreted depends on the build system, but any setuptools-based build system (including most instances of setup.py) will likely just use them as a hook for optional dependencies.

It's worth noting that the syntax supports using multiple keywords, e.g.:

pip install "splinter[django,flask,selenium]"

Kudos to @chepner for adding context in the comments.




ANSWER 2

Score 143


Brackets [optional] in PIP signify optional dependencies

Just in case another developer comes along looking to implement this pattern in their own Python package deployment, here's further explanation of the brackets [] in pip.

For Example: Apache Airflow

To install airflow from pip we use this command:

pip install 'apache-airflow'

You can install optional components of airflow with:

pip install 'apache-airflow[aws]'
#      [optional] -----------^

When we search pypi for apache-airflow note that the optional packages do not show up:

pip search 'apache-airflow'

apache-airflow (1.10.9)                            - Programmatically author, schedule and monitor data pipelines
pylint-airflow (0.1.0a1)                           - A Pylint plugin to lint Apache Airflow code.
swe-airflow-tools (0.0.3)                          - Tools for Apache Airflow Application
airflow (0.6)                                      - Placeholder for the old Airflow package
...

Implementation via setup.py

You can see how this was accomplished in the setup.py script
On the left in setup.py - extras_require is defined.
On the right are the correlated installation commands for these optional sub-packages.

setup.py vs install




ANSWER 3

Score 32


Maybe worthwhile to know that this optional package syntax admits multiple extras (separated by comma within the brackets) as in:

python -m pip install SomePackage[PDF,EPUB]  # multiple extras

As per the pip manual




ANSWER 4

Score 31


Pretty sure these are setuptools extras:

https://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-extras-optional-features-with-their-own-dependencies

Sometimes a project has “recommended” dependencies, that are not required for all uses of the project. For example, a project might offer optional PDF output if ReportLab is installed, and reStructuredText support if docutils is installed. These optional features are called “extras” ...