title: Dynamically loading modules at runtime in Python 3
body: <p>Which is the recommended way to lazy load files in Python 3?</p>
  
  <p>I've built this function by copy-past from Python 2 code</p>
  
  <pre><code>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']
  </code></pre>
  
  <p>but I'm not sure if it is the right way. It uses <code>compile</code> and <code>eval</code></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After using <code>import</code> like this:</p>
  
  <pre><code>def get_command(self, ctx, cmd_name):
      cmd_mod = import_module('{}.cmd_{}'.format(cmd_folder, cmd_name))
  </code></pre>
  
  <p>this is the result:</p>
  
  <pre><code>  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 "&lt;frozen importlib._bootstrap&gt;", line 994, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 971, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 941, in _find_and_load_unlocked
    File "&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
    File "&lt;frozen importlib._bootstrap&gt;", line 994, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 971, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 953, in _find_and_load_unlocked
  ModuleNotFoundError: No module named '/Users/salvatoremazzarino/awsiam/aws_iam_cli/commands'
  </code></pre>
  
  <p><strong>EDIT #2:</strong></p>
  
  <pre><code>def get_command(self, ctx, cmd_name):
          mod = import_module('aws_iam_cli.commands.cmd_{}'
                              .format(cmd_name))
          return mod.cli
  </code></pre>
  
  <p>and the error:</p>
  
  <pre><code>  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 "&lt;frozen importlib._bootstrap&gt;", line 994, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 971, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 941, in _find_and_load_unlocked
    File "&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
    File "&lt;frozen importlib._bootstrap&gt;", line 994, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 971, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 941, in _find_and_load_unlocked
    File "&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
    File "&lt;frozen importlib._bootstrap&gt;", line 994, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 971, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 953, in _find_and_load_unlocked
  ModuleNotFoundError: No module named 'aws_iam_cli'
  </code></pre>
  
  <p>This is the dir tree:</p>
  
  <pre><code>├── aws_iam_cli
  │   ├── __init__.py
  │   ├── cli.py
  │   ├── commands
  │   │   ├── __init__.py
  │   │   ├── __pycache__
  │   │   └── cmd_dump.py
  │   └── provider
  │       ├── __init__.py
  │       ├── policy.py
  │       └── role.py
  </code></pre>
  
  <p>Inside commands I have a module and I call inside that module:</p>
  
  <pre><code>from aws_iam_cli.provider.role import fetch_roles
  </code></pre>