如何动态导入更改模块?

哈哈哈哈

步骤1:假设这是正在更改的模块,名为test_module.py

#coding=utf8
# changing module    

class hello(object):
    pass


"""
class world(object):
    pass
"""

第2步:动态重新加载changing module名为dynamis_changing_import.py的

"""
@note: Dynamicly import changing module
"""

__author__ = 'k9 Lee'


import sys
import importlib


"""
@important: Does not consider thread safty
"""

def dy_import(name, package=None):
    # Fast path: see if the module has already been imported.
    # @doc: imp
    try:
        sys.modules[name]
    except KeyError:
        # if not found, just import
        importlib.import_module(name, package)
    else:
        # del and reload module
        print '1'
        del sys.modules[name]
        print '2'
        #importlib.import_module(name, package)
        #print '3'

步骤3:在ipython中进行测试,

然后我发现了sys.modules['test_module']引发关键错误,这意味着我可以重新导入test_module,

但我仍然可以dir(test_module)...

In [1]: import test_module

In [2]: dir(test_module)
Out[2]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'hello']

In [3]: import dynamis_changing_import

In [4]: dynamis_changing_import.dy_import('test_module')
1
2

In [5]: import sys

In [6]: sys.modules['test_module']  # Here, test_module does not exist.
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-6-d2451de5c425> in <module>()
----> 1 sys.modules['test_module']

KeyError: 'test_module'

In [7]: dir(test_module)  # But... dir... is still there...
Out[7]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'hello']

步骤4.仍测试ipython:我取消注释文件中的最后两个语句dynamis_changing_import.py

importlib.import_module(name, package)
print '3'

并且importlib.import_module没有任何效果,我重新启动了ipython:

In [1]: import sys

In [2]: import dynamis_changing_import

In [3]: import test_module

In [4]: dir(test_module)
Out[4]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'hello']

In [5]: # Uncomment the method `world` in test_module

In [6]: dynamis_changing_import.dy_import('test_module')
1
2
3

In [7]: dir(test_module)
Out[7]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'hello']

谢谢你。

巴库留

不要惹sys.modules这是一个非常低级的细节。

要安全地重新加载模块,您可以:

对于跨版本的解决方案,只需执行以下操作:

import sys
if sys.version_info.major == 3:
    if sys.version_info.minor < 4:
        from imp import reload
    else:
        from importlib import reload

当您想重新导入模块时X,只需执行以下操作:

reload(X)

因此,“动态导入”变为:

import moduleX

每当您需要重新加载模块时,您都可以执行以下操作:

reload(moduleX)

样品运行:

$ echo 'def f():print("a")
> f()' > t.py
$ python2
>>> import t
a
>>> t.f()
a
# in an other shell:
# $ echo 'def f():print("b")
# > f()' > t.py
>>> reload(t)
b
>>> t.f()
b

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章