Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do square brackets mean in pip install?

People also ask

What is square brackets in pip install?

The square bracket contains the 'extra' option's information defined in setup.py that pip will use to install additional dependencies.

What is the flag for pip install?

The --user flag to pip install tells Pip to install packages in some specific directories within your home directory. This is a good way to have your own default Python environment that adds to the packages within your system directories, and therefore, does not affect the system Python installation.

What does stand for in pip install?

Package Installer for Python (pip) is the de facto and recommended package-management system written in Python and is used to install and manage software packages. It connects to an online repository of public packages, called the Python Package Index.

How does a pip install work?

The pip install <package> command always looks for the latest version of the package and installs it. It also searches for dependencies listed in the package metadata and installs them to ensure that the package has all the requirements that it needs.


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. The square brackets ([]) are not specific syntax, just convention. Really, you are installing the package named: "splinter[django]".

An explanation from @chetner:

The command pip install splinter django would install two packages named splinter and django. splinter[django], on the other hand, installs a variant of the splinter package which contains support for django. Note that it has nothing to do with the django package itself, but is just a string defined by the splinter package for a particular feature set that gets enabled.


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


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” ...


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


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"],