从绝对路径导入模块而不修改sys.modules

时间:2017-08-10 21:00:15

标签: python import module

所以,我知道我可以将一个文件路径的python模块加载到模块中

def import_module_from_fpath(module_fpath):
    from os.path import basename, splitext, isdir, join, exists, dirname, split
    import platform
    if isdir(module_fpath):
        module_fpath = join(module_fpath, '__init__.py')
    print('module_fpath = {!r}'.format(module_fpath))
    if not exists(module_fpath):
        raise ImportError('module_fpath={!r} does not exist'.format(
            module_fpath))
    python_version = platform.python_version()
    modname = splitext(basename(module_fpath))[0]
    if modname == '__init__':
        modname = split(dirname(module_fpath))[1]
    if python_version.startswith('2.7'):
        import imp
        module = imp.load_source(modname, module_fpath)
    elif python_version.startswith('3'):
        import importlib.machinery
        loader = importlib.machinery.SourceFileLoader(modname, module_fpath)
        module = loader.load_module()
        # module = loader.exec_module(modname)
    else:
        raise AssertionError('invalid python version={!r}'.format(
            python_version))
return module

但是,这似乎总是填充sys.modules

中的条目

我的情况是我在不同的路径中有两个不同的模块。 PYTHONPATH无法访问这两个模块。我希望能够在不同的功能中本地加载这些模块,而不是让它们相互干扰。这个问题的关键是两个模块都有相同的名称(不同的版本)。因此,我不希望sys.modules缓存其中的任何内容。

我不确定加载后是否可以安全地从sys.modules删除模块名称。看起来它可能来自我做的快速测试:

    import shutil
    import ubelt as ub
    test_root = ub.ensure_app_cache_dir('test_fpath_import')
    # Clear the directory
    shutil.rmtree(test_root)
    test_root = ub.ensure_app_cache_dir('test_fpath_import')

    # -----
    # Define two temporary modules with the same name that are not in sys.path
    import sys, os, os.path
    from os.path import join

    # Even though they have the same name they have different values
    mod1_fpath = ub.ensuredir((test_root, 'path1', 'testmod'))
    ub.writeto(join(mod1_fpath, '__init__.py'), 'version = 1\nfrom . import sibling\na1 = 1')
    ub.writeto(join(mod1_fpath, 'sibling.py'), 'spam = \"ham\"\nb1 = 2')

    # Even though they have the same name they have different values
    mod2_fpath = ub.ensuredir((test_root, 'path2', 'testmod'))
    ub.writeto(join(mod2_fpath, '__init__.py'), 'version = 2\nfrom . import sibling\na2 = 3')
    ub.writeto(join(mod2_fpath, 'sibling.py'), 'spam = \"jam\"\nb2 = 4')

    # -----
    # Neither module should be importable through the normal mechanism
    try:
        import testmod
        assert False, 'should fail'
    except ImportError as ex:
        pass

    mod1 = import_module_from_fpath(mod1_fpath)
    print('mod1.version = {!r}'.format(mod1.version))
    print('mod1.version = {!r}'.format(mod1.version))
    print(mod1.version == 1, 'mod1 version is 1')
    print('mod1.a1 = {!r}'.format(mod1.a1))

    mod2 = import_module_from_fpath(mod2_fpath)
    print('mod2.version = {!r}'.format(mod2.version))
    print(mod2.version == 2, 'mod2 version is 2')
    print('mod2.a2 = {!r}'.format(mod1.a2))

    # BUT Notice how mod1 is mod2
    print(mod1 is mod2)

    # mod1 has attributes from mod1 and mod2
    print('mod1.a1 = {!r}'.format(mod1.a1))
    print('mod1.a2 = {!r}'.format(mod1.a2))
    print('mod2.a1 = {!r}'.format(mod2.a1))
    print('mod2.a2 = {!r}'.format(mod2.a2))

    # Both are version 2
    print('mod1.version = {!r}'.format(mod1.version))
    print('mod2.version = {!r}'.format(mod2.version))

    # However sibling always remains at version1 (ham)
    print('mod2.sibling.spam = {!r}'.format(mod2.sibling.spam))

    # now importing testmod works because it reads from sys.modules
    import testmod

    # reloading mod1 overwrites attrs again
    mod1 = ut.import_module_from_fpath(mod1_fpath)

    # Removing both from sys.modules
    del sys.modules['testmod']
    del sys.modules['testmod.sibling']
    mod2 = ut.import_module_from_fpath(mod2_fpath)

    print(not hasattr(mod2, 'a1'),
        'mod2 no longer has a1 and it reloads itself correctly')

上面的脚本说明了在尝试加载具有相同名称的两个模块不同模块时会发生什么:它们相互覆盖/互补。但是,如果我执行此导入然后直接删除所有导入的模块,它似乎执行"右"事情。

我的问题是:

  1. 它是否真的在做正确的事情"事情?或者有些情况我的脚本没有测试。

  2. 有没有办法在本地导入模块而不将其添加到sys.modules(又称全局命名空间)? (sys.modules是唯一构成模块全局命名空间的东西吗?)

  3. (我只测试了Python 3中的上述代码,不确定旧的imp模块是否与importlib完全相同。

0 个答案:

没有答案