Reputation: 15090
I see more and more commands like this:
$ pip install "splinter[django]"
What do these square brackets do?
Upvotes: 290
Views: 75607
Reputation: 20434
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.
Upvotes: 197
Reputation: 371
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
Upvotes: 33
Reputation: 2196
Except extras_require
in setup.py, this extra dependences maybe specified in [project.optional-dependencies]
of pyproject.toml too.
Upvotes: 0
Reputation: 353
The square bracket contains the 'extra' option's information defined in setup.py that pip will use to install additional dependencies.
pip install "splinter[django]"
To be specific, the above line will install first the 'splinter' package, then install the extra dependencies the 'splinter' project requires with the 'django' option specified in a setup.py of 'splinter' project.
pip install "splinter[django]"
pip install "splinter" "Django>=2.0.6" "lxml>=4.2.4" "cssselect"
As of splinter==0.16.0, with python==3.9.2, the above two commands are equivalent.
Both pip install will result in the following packages given a clean virtual enviroment.
The reason why the two pip install commands achieve same is because this is literally what has been run in the background based on the setup.py of the splinter package
The '[django]' is the 'extra' option for the 'splinter' package. Pip will look into the setup.py of splinter package, and find what needs to be installed with the '[django]' option specified. In this case, it is these 3 packages: ["Django>=2.0.6", "lxml>=4.2.4", "cssselect"]
Upvotes: 19
Reputation: 3614
[optional]
in PIP signify optional dependenciesJust 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.
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
...
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.
Upvotes: 145
Reputation: 46449
This is exactly the list from the setup.py
file for the project in question:
"django": ["Django>=1.7.11;python_version<'3.0'", "Django>=2.0.6;python_version>'3.3'", "lxml>=2.3.6", "cssselect", "six"],
Upvotes: 6
Reputation: 1242
Pretty sure these are setuptools extras:
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” ...
Upvotes: 31