title: Imports with complex folder hierarchy
body: <p>Hey I'm working on a project that has a set hierarchical modules with a folder structure set up like so:</p>
  
  <pre><code>module_name/
      __init__.py
      constants.py
      utils.py
      class_that_all_submodules_need_access_to.py
  
      sub_module_a/
          __init__.py
          sub_module_a_class_a.py
          sub_module_a_class_b.py
          useful_for_sub_module_a/
               __init__.py
               useful_class_a.py
               useful_class_b.py
  
      sub_module_b/
          __init__.py
          sub_module_b_class_a.py
          sub_module_b_class_b.py
  </code></pre>
  
  <p>etc etc etc...</p>
  
  <p>The problem is, I can't figure out how to set up the imports in the <strong>init</strong>.py's so that I can access class_that_all_submodules_need_access_to.py from sub_module_a/useful_for_sub_module_a/useful_class_a.py.</p>
  
  <p>I've tried looking this up on Google/StackOverflow to exhaustion and I've come up short. The peculiar thing is that PyCharm has the paths set up in such a way that I don't encounter this bug when working on the project in PyCharm, but only from other environments.</p>
  
  <p>So here's one particularly inelegant solution that I've come up with. My sub_module_a/useful_for_sub_module_a/<strong>init</strong>.py looks like:</p>
  
  <pre><code>import sys
  import os
  
  sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')))
  
  import module_name
  </code></pre>
  
  <p>This is similar in sub_module_*/, where instead of 3 '..'s it's just two (i.e. '..', '..' instead of '..', '..', '..' for the sys.path.insert line above). And then in sub_module_a/useful_for_sub_module_a/useful_class_a.py, I have to import module_name/constants.py (and others) like this:</p>
  
  <pre><code>import module_name.constants
  from module_name.class_that_all_submodules_need_access_to import ImportantClass
  </code></pre>
  
  <p>While this solution works, I was wondering if there is a <strong>better/more elegant way to set up the imports and/or folder hierarchy</strong>? I'm concerned about <strong>messing with the python system path for users</strong> of this module. Is that even a valid concern? </p>