Get 2 isolated instances of a python module

前端 未结 5 993
逝去的感伤
逝去的感伤 2021-02-05 14:13

I am interacting with a python 2.x API written in a non-OO way, it uses module-global scope for some internal state driven stuff. It\'s needed in a context where it\'s no longer

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 14:57

    This can be achieved by importing the module via different paths. That is - if in your sys.path you have two different dotted routes to the module, the module cache will create two different instances of the module with different symbol trees, globals and so on.

    This can be used to have multiple versions of a library also.

    Beware that it will lead to exceptions not being caught (as you are trying to catch the wrong symbol).

提交回复
热议问题