Pip Install Optional Dependency

·

1 min read

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.

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

Command Line

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:

pip install cvnn

It also provides plotting functions:

pip install cvnn[plotter]

Source Code

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

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