在Python 3中在运行时动态加载模块

时间:2017-10-30 09:40:46

标签: python

在Python 3中延迟加载文件的推荐方法是什么?

我已经通过Python 2代码中的copy-past构建了这个函数

def get_command(self, ctx, cmd_name):
        ns = {}
        fn = os.path.join(cmd_folder, 'cmd_{}.py'.format(cmd_name))

        with open(fn) as f:
            code = compile(f.read(), str(fn), 'exec')
            eval(code, ns, ns)

        return ns['cli']

但我不确定这是不是正确的方法。它使用compileeval

修改

使用import之后:

def get_command(self, ctx, cmd_name):
    cmd_mod = import_module('{}.cmd_{}'.format(cmd_folder, cmd_name))

这是结果:

  File "aws_iam_cli/cli.py", line 23, in get_command
    cmd_mod = import_module('{}.cmd_{}'.format(cmd_folder, cmd_name))
  File "/Users/salvatoremazzarino/awsiam/venv/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named '/Users/salvatoremazzarino/awsiam/aws_iam_cli/commands'

编辑#2:

def get_command(self, ctx, cmd_name):
        mod = import_module('aws_iam_cli.commands.cmd_{}'
                            .format(cmd_name))
        return mod.cli

和错误:

  File "/Users/salvatoremazzarino/awsiam/venv/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'aws_iam_cli'

这是目录树:

├── aws_iam_cli
│   ├── __init__.py
│   ├── cli.py
│   ├── commands
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   └── cmd_dump.py
│   └── provider
│       ├── __init__.py
│       ├── policy.py
│       └── role.py

内部命令我有一个模块,我在该模块内部调用:

from aws_iam_cli.provider.role import fetch_roles

1 个答案:

答案 0 :(得分:2)

您可以使用importlib.import_module在运行时动态加载模块。例如:

from importlib import import_module

# load module
pckg_name = 'commands'  # I assume all your commands are in this pckg
cmd_mod = import_module('{}.cmd_{}'.format(pckg_name, cmd_name))

# run command (I'm assuming the module contains a function with the 'cmd_name')
cmd_mod.cmd_name()

我认为这种方法会更清洁,更安全,并且应该与你正在做的事情一起工作。