Reputation: 133
Airflow module comes with a main pip package apache-airflow
and multiple subpackages to install only what we need (check doc https://airflow.incubator.apache.org/installation.html#extra-packages)
I would like to test if a subpackage is installed or not (and for example get a bash command to know if apache-airflow[mysql]
is installed).
To know if apache-airflow
is installed, I can do pip show apache-airflow
and get 0
as exit code if installed, 1
otherwise. If I do pip show apache-airflow['mysql']
, I always get 1
as exit code, the subpackage being installed or not.
And I could not find any option for that. Any idea?
Upvotes: 4
Views: 1703
Reputation: 69962
The "subpackage" you're referring to here is called a "setuptools extra". pip does not record these anywhere in the filesystem. You can however detect whether they are installed by iterating the installation metadata and testing whether each dependent package is installed.
Fortunately, there's a method in pkg_resources
which does this for you
I tested with jsonschema[format]
as it was easy for me to install / uninstall.
jsonschema[format]
installed$ python -c 'import pkg_resources; pkg_resources.require("jsonschema[format]")'
$ echo $?
0
[format]
extra$ python -c 'import pkg_resources; pkg_resources.require("jsonschema[format]")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 967, in require
needed = self.resolve(parse_requirements(requirements))
File "venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 853, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'webcolors; extra == "format"' distribution was not found and is required by jsonschema
$ echo $?
1
Upvotes: 5