4

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?

1 Answer 1

5

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.

with jsonschema[format] installed

$ python -c 'import pkg_resources; pkg_resources.require("jsonschema[format]")'
$ echo $?
0

without the dependencies of the [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
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to access this extras inside setup.py? I wan't to include different packages based on the provided extra.
@Jack a bit unrelated to this question but that's extras_require

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.