Auto-install missing python modules

I just learned about a neat little python feature introduced in python 3.3 that lets you hook into python’s default import machinery. First a demo:

Ok, so how’s that done ? Well, there are 2 parts to it. Firstly creating the import hook. This is exploiting the new importlib feature for registering a MetaPathFinder class:

The standard lib documentation is very detailed in explaining the how of this mechanism but if you’d like to understand the why and explore other reasons/benefits of being able to hook into the import mechanism you should take some time to read the related PEPs.

The second part to this ensuring that it’s loaded into your environment. The has been done in the demo defining the hook above in the PYTHONSTARTUP file.

If you liked this neat little trick and are interested in other such useful hacks, do check out my custom python startup. Comments and suggestion are always appreciated.