Skip to main content

Command Palette

Search for a command to run...

Pip Install Optional Dependency

Published
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

More from this blog

簡介 C++ 的 Type Erase (用多型和模板做 Duck Type)

起點 讓我們先從 template 出發:foo 需要一個 callback function。 template<typename Func> void foo(Func callback) { // ... callback(); } 但是這會讓編譯錯誤訊息有點模糊:假如 callback 並不是一個可以呼叫的函數指標,或者並不是一個 callable object ,那編譯器會說錯出在第四行。但是我們都希望,編譯器在呼叫函數時就幫我們指出:這不是 foo 想要的 call...

May 14, 20243 min read

帕秋莉的魔法筆記

45 posts

後端工程師。

不定時張貼一些寫扣時的筆記。