title: Dynamically import two files into another file in python which those two files are in the dufferent directories
body: <p>I am trying to import the modules dynamically using python. My directory structure is</p>
  <p>root_folder1:<br>
  -sub_folder1<br>
  -sub_folder2<br />
  ---sub_sub_folder<br />
  -my_file1.py<br />
  -my_file2.py<br />
  -my_main_file.py</p>
  <p>I have to import my_file1.py and my_file2.py(which is inside sub_Sub_folder) methods into my_main_file.py(inside the root_folder1). I am running my_main_file.py in the root_folder only.</p>
  <p>To dynamically import those two file I tried using importlib and <strong>import</strong>. But I got an error 'no module named &quot;</p>
  <p>My code in my_main_file.py is:</p>
  <pre><code>import os, fnmatch
  from pathlib import Path
  import importlib
  import sys
  sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  
  
  listoffiles = os.listdir(&quot;sub_folder2/sub_sub_folder/&quot;)
  pattern = &quot;*.py&quot;
  
  abs_path = &quot;sub_folder2/sub_sub_folder/&quot;
  for entry in listoffiles:
      if fnmatch.fnmatch(entry, pattern):
          print(entry,abs_path)
  
          try:
              spec = importlib.util.spec_from_file_location(entry, abs_path)
              print(spec)
              foo = importlib.util.module_from_spec(spec)
              spec.loader.exec_module(foo)
              print(f'fp:{foo}{spec}')
          except Exception as e:
              print(f'The error is {e}')
  </code></pre>
  <p>i tried using the importlib.import_module(entry,package=abs_path). I got an error.</p>
  <p>When I run the above code I am getting &quot;The error is 'NoneType' object has no attribute 'loader'&quot;.</p>
  <p>My question is:
  How do I dynmaically import the files my_file1.py,my_file2.py into my_main_file.py and uses the fucntions of my_file1 and my_file2.</p>
  <p>I tried <a href="https://stackoverflow.com/questions/19009932/import-arbitrary-python-source-file-python-3-3">Import arbitrary python source file. (Python 3.3+)</a> and I am getting no module error</p>