【问题标题】:ModuleNotFoundError: No module named - PythonModuleNotFoundError: No module named - Python
【发布时间】:2022-12-19 10:08:14
【问题描述】:

Can someone please help with this error? I have tried all sort of ways to import a module in Python but not has worked so far. I have this directory structure below:

|__folder1:
|      |__folder1a
|          |___inner_file.py
|__outer_file.py

I want to import a Class from the outer_file.py into the inner_file.py. However, I have been getting the error ModuleNotFoundError: No module named.

I have tried all the options in this link on GeeksforGeeks. Thanks.

【问题讨论】:

    标签: python python-import modulenotfounderror


    【解决方案1】:

    easy

    import sys,os
    BASE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.insert(0, BASE)
    

    【讨论】:

      【解决方案2】:

      Your solution works well for me. But I still not understand how works your solution. Can you give me some explanation please.

      import sys,os BASE = os.path.dirname(os.path.dirname(os.path.abspath(file))) sys.path.insert(0, BASE)

      【讨论】: