I'm working on python packages that implement scientific models and I'm wondering what is the best way to handle optional features. Here's the behavior I'd like: If some optional dependencies can't be imported (plotting module on a headless machine for example), I'd like to disable the functions using these modules in my classes, warn the user if he tries to use them and all that without breaking the execution. so the following script would work in any cases: mymodel.dostuff() mymodel.plot() <= only plots if possible, else display log an error mymodel.domorestuff() <= get executed regardless of the result of the previous statement So far the options I see are the following: check in the __init __.py for available modules and keep a list of them (but how to properly use it in the rest of the package?) for each function relying on optional dependencies have a try import ... except ... statement putting functions depending on a particular module in a separated file These options should work, but they all seem to be rather hacky and hard to maintain. what if we want to drop a dependency completely? or make it mandatory? Continue reading...