39
votes

When installing my python package, I want to be able to tell the user about various optional dependencies. Ideally I would also like to print out a message about these optional requirements and what each of them do.

I haven't seen anything yet in the docs of either pip or docutils. Do tools these support optional dependencies?

2
If they're optional, they're not strictly dependencies, are they? - Vinay Sajip
I call them optional dependencies, because that is what ubuntu's package manager call them. They are not strictly required, but if they are installed, the program can use them. - Mike Cooper
Not a duplicate of 3664478, the other asker explicitly wanted pip-requirements which isn't a very natural way to handle this. - Tobu

2 Answers

37
votes

These are called extras, here is how to use them in your setup.py.

The base support is in pkg_resources. You need to enable distribute in your setup.py. pip will also understand them:

pip install 'package[extras]'
8
votes

Yes, at stated by @Tobu and explained here. In your setup.py file you can add a line like this:

extras_require = {
        'full': ['matplotlib', 'tensorflow', 'numpy', 'tikzplotlib']
    }

I have an example of this line here.

Now you can either install via PIP basic/vanilla package like pip install package_name or the package with all the optional dependencies like pip install package_name[full]

Where package_name is the name of your package and full is because we put "full" in the extras_require dictionary but it depends on what you put as a name.


If someone is interested in how to code a library that can work with or without a package I recommend this answer