Get 2 isolated instances of a python module

前端 未结 5 1044
逝去的感伤
逝去的感伤 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:45

    Just remove the module from sys.modules:

    >>> import sys
    >>> import mod as m1
    >>> m1.x = 1
    >>> del sys.modules['mod']
    >>> import mod as m2
    >>> m2.x = 2
    >>> m1.x
    1
    

提交回复
热议问题