title: Python - import a module directory given full path
body: <p>I have a python module here <code>/root/python/foo.py</code>. I have a bunch of other modules here in the folder <code>/root/lib/</code> which looks like this</p>
  
  <pre><code>
      lib/
      |
      ├─ module1/
      |  ├─ __init__.py
      |  └─ bar.py
      |
      └─ module2/
         ├─ __init__.py
         └─ bar.py
  
  </code></pre>
  
  <p>I would like to import <code>/root/lib/module1</code> and <code>/root/lib/module2</code> from <code>foo.py</code>. I would like to <strong>not</strong> have to add <code>/root/lib/</code> to the python system path. <a href="https://stackoverflow.com/a/67692/1460845">This stack overflow answer</a> tells you how to use either <code>imp.load_source</code>, <code>importlib.machinery.SourceFileLoader</code>, or the <code>importlib.util</code> class to load a module from a file (depending on the python version). I think these only work if the module is a single file. If I try something like this in Python 3.4</p>
  
  <pre><code>from importlib.machinery import SourceFileLoader
  problem_module = SourceFileLoader('test_mod', '/root/lib/module1').load_module()
  </code></pre>
  
  <p>I get an <code>IsADirectoryError</code></p>
  
  <p>My question is whether there is a similar way to load a module (given its full path) if it is a directory, without adding the whole <code>lib/</code> folder to the system path?</p>