Search code examples
python-3.xpython-module

Getting a ModuleNotFoundError when trying to load a Python Module and all seems ok


I have a tree structure

 - My_app    
   - __init__.py
   - main.py
   - Cus_Scripts  
      - __init__.py
      - script1.py
      - script2.py - classA

I am running like this

  • main.py has import Cus_Scripts.script1
  • script1.py has from script2 import classA

in script1 I get a ModuleNotFoundError saying it can't find script2. I am using VSC.


Solution

  • I fixed this by adding this to every file.

    
    #Custom Lib
    sys.path.insert(1, os.getcwd())
    
    

    This allowed me to access anypart of the program with

    
    from My_App.script2 import classA
    import My_App.script1