title: The simple answer to importing Python modules
body: <p>The problem/question of importing modules have been asked a lot in stackoverflow but they are usually filled with multiple convoluted answers. Is there any guru out here who can just provide a short direct solution?</p>
  
  <p>So I have my directory structure here and without using changing <code>sys.path</code> or using <code>PYTHONPATH</code>. </p>
  
  <p>How do I </p>
  
  <p><strong>a.</strong> Allow all files in <strong>dir_a</strong> and <strong>dir_b</strong> be able to import each other's functions</p>
  
  <p><strong>b.</strong> Allow all scripts in <strong>script_group1</strong> and <strong>script_groupt2</strong> to import any module in <strong>dir_a</strong> and <strong>dir_b</strong></p>
  
  <pre><code>my_scripts
    |_ script_group1
      |_ script1.py
      |_ script2.py
    |_ script_group2
      |_ script3.py
      |_ script4.py
  my_modules
    |_ dir_a
      |_ file_a.py
      |_ file_b.py
    |_ dir_b
      |_ file_c.py
      |_ file_d.py
  </code></pre>
  
  <p>EDIT:
  Ok so after waiting a few days and no one seems to have a good answer or explanation, I decided to accept the fact that this is the way Python is. The cleanest possible solution for my case would be to edit sys.path but only in the python files in the "my_scripts" directory. These would be the "top-level" scripts that will import common modules in the "my_modules" directory. Any common functions will only be written in modules in "my_modules" directory. No common functions will be written in "my_scripts"</p>
  
  <p>Inter-module importing between modules in "my_modules" will be done using relative imports. While scripts in "my_scripts" will perform absolute importing of modules in "my_modules" with </p>
  
  <pre><code>sys.path.insert(0, path_to_my_modules)
  </code></pre>
  
  <p>In this way the contamination and damage is limited only to scripts in "my_scripts" whereas using PYTHONPATH will cause a global system wide epidemic.</p>