Let's say I have modules foo.py
and bar.py
and both have import baz
statement in their code.
I want to write an import hook that I can insert into sys.meta_path
that knows whether baz
is being imported from foo
or from bar
.
The reason why I want to do this is because I want my app to be able to load plugins and keep their dependencies isolated, the way I would be able to by using separate ClassLoader
instances in Java. That way, plugin foo
and plugin bar
would be able to bundle different versions of library baz
without conflict. The way I mean to accomplish that is by changing the import mechanism to produce separate modules for import baz
statement depending on where that statement comes from.
If anyone has a better way of doing this, feel free to suggest it in your answer.