DEV Community

Dependency Injection with Import Hooks in Python 3

Kermit Alexander II on December 26, 2018

sys.meta_path is one of the best-kept secrets in the Python standard library, despite being squarely located in a module every Python programmer is...
Collapse
 
rhymes profile image
rhymes

Hi Kermit, super interesting. I didn't know about sys.meta_path - seems also very handy to develop plugin systems like you did! Great for a big customizable app

A small suggestion: if you re-edit your post you can add syntax highlighting with tags like this:

>>> import platform
>>> platform.python_version()
'3.7.1'
Enter fullscreen mode Exit fullscreen mode

You just need to add python at the end of the three beginning ticks, like this:

Collapse
 
dangerontheranger profile image
Kermit Alexander II

Ah, appreciate the tip on syntax highlighting! I've updated the article accordingly.

Collapse
 
florimondmanca profile image
Florimond Manca

Interesting post! Thanks.

Have you tried seeing how static type checking behaves with this meta_path trick? Or would IDEs complain that the myapp.virtual.foo module does not exist?