title: Organizing packages that provide functionality to each other and to the main program
body: <p>I have the following structure on a Python program:</p>
  
  <pre><code>my_program/
      main.py
      packages/
          __init.py__
          package_to_share/
             __init__.py
              main_to_share.py
              module_to_share.py
          package_A/
             __init__.py
              main_A.py
              some_module_A.py
          package_B/
             __init__.py
              main_B.py
              some_module_B.py
  </code></pre>
  
  <p>The package <code>package_to_share</code> provides functionality that every package in the <code>packages</code> folder uses and that <code>main.py</code> at the root folder uses.</p>
  
  <p>I also want to be able to <code>cd</code> into each package and be able to run <code>main_X.py</code>. </p>
  
  <p>So far I figured out how to access functionality <strong>from <code>main.py</code></strong>:</p>
  
  <pre><code>import packages.package_A.some_module_A
  import packages.package_to_share.module_to_share
  </code></pre>
  
  <p>but I am having problems accessing the functionality in <code>package_to_share</code> from regular packages (e.g. <code>package_A</code>)</p>
  
  <p>For example, when in <code>main_A.py</code> or <code>some_module_A.py</code>, typing <code>import packages.package_to_share.module_to_share</code> fails.</p>
  
  <p>This leads me to following questions questions:</p>
  
  <ol>
  <li><p>Given the specifics of my problem, with packages to be shared (accessed) by files at the root folder and by other packages, is there a better way to organize my folders ? Does this organization of modules and files in general conform to good standards in Python? </p></li>
  <li><p>The following looks incredibly hacky to me, but it's the best thing I came up with to make sure my regular modules see the <code>"shared"</code> modules:</p>
  
  <pre><code>p_this_file   = os.path.dirname(os.path.realpath(__file__))
  new_sys_path.append(os.path.join(p_cwd, '..')
  new_sys_path.extend(sys.path)
  sys.path      = new_sys_path
  </code></pre>
  
  <p>It also does not prevent my regular packages from importing each other.</p></li>
  </ol>