I would like to be able to write:
try:
import foo
except ImportError:
install_the_module("foo")
What is the recommended/idiomatic way to handle this scenario?
I’ve seen a lot of scripts simply print an error or warning notifying the user about the missing module and (sometimes) providing instructions on how to install. However, if I know the module is available on PyPI, then I could surely take this a step further an initiate the installation process. No?
Installation issues are not subject of the source code!
You define your dependencies properly inside the
setup.pyof your packageusing the
install_requiresconfiguration.That’s the way to go…installing something as a result of an
ImportErroris kind of weird and scary. Don’t do it.