title: How to import a module from a subfolder in python3 (with empty __init__.py)
body: <p>I want to call a function from a file in a sub folder. I would prefer do do it statically. I use empty <code>__init__.py</code> files as I've read that in simple cases they can be empty(and mine couldn't be an simpler) or that from 3.5 on I don't need them at all. But I am open to filling them.</p>
  
  <p>I have the following file structure:</p>
  
  <pre><code>test
  ├── callfoo.py   (main)
  ├── __init__.py  (empty)
  └── folder
      ├── submodule.py
      └── __init__.py (empty)
  </code></pre>
  
  <p>callfoo.py:</p>
  
  <pre><code>#import statement wanted
  
  def main():
      foo()
  
  if __name__ == "__main__":
          main()
  </code></pre>
  
  <p>submodule.py</p>
  
  <pre><code>def foo():
      print('foo')
  </code></pre>
  
  <p>For the import statement I've tried:</p>
  
  <p><code>import test.folder.submodule</code><br>
  <code>from test.folder import submodule</code><br>
  <code>from test.folder.submodule import foo</code> </p>
  
  <p>each resulting in <code>ModuleNotFoundError: No module named 'test.folder'</code>
  I'm a bit confused here because they are taken straight from the <a href="https://docs.python.org/3/tutorial/modules.html#intra-package-references" rel="noreferrer">Documentation</a></p>
  
  <p><code>import .folder.submodule</code> -> <code>invalid syntax</code>
  <code>importlib.import_module('test.folder.submodule')</code>
  ModuleNotFoundError: No module named 'test.folder'</p>
  
  <p>This works:</p>
  
  <pre><code>import importlib.util
  spec = importlib.util.spec_from_file_location("submodule", "/home/.../test/folder/submodule.py")
  foo = importlib.util.module_from_spec(spec)
  spec.loader.exec_module(foo)
  </code></pre>
  
  <p>But I don't really want to do it dynamically especially not for several files.</p>
  
  <p>This <a href="https://stackoverflow.com/questions/43728431/modulenotfounderror-no-module-named-x">ModuleNotFoundError: No module named x</a> deals with my error message but not with subfolders as far as I can tell (it works fine for me with <code>submodule.py</code> on the same level as <code>callfoo.py</code>)</p>
  
  <p>There are several questions dealing with imports from sub folders but I couldn't make them work for me.
  I hope to have provided a dead simple problem formulation.</p>