Skip to content

Pip Install Optional Dependency ​

Sometimes, the users may not need to install all dependencies of a package, since they will not use the part of it.

We can assign some optional dependencies for a python package.

How ​

setup.py ​

Add the extras_require parameter in the setup function.

python
setup(
    # ...
    extras_require={
        'plotter': ['matplotlib'],
        'full': ['matplotlib', 'tensorflow'],
    },
}

Command Line ​

bash
pip install package_name
pip install package_name[plotter] # Install matplotlib
pip install package_name[full] # Install matplotlib, tensorflow

CVNN ​

Take CVNN, a python package, as an example:

Document ​

By its document, we can install minimum dependencies:

bash
pip install cvnn

It also provides plotting functions:

bash
pip install cvnn[plotter]

Source Code ​

In python source codes, the package put import part into a try block:

python
try:
    import plotly
    import plotly.graph_objects as go
    import plotly.figure_factory as ff
    AVAILABLE_LIBRARIES.add('plotly')
except ImportError as e:
    logger.info("Plotly not installed,...")

Reference ​

Just observe 👀