Python - Doing absolute imports from a subfolder

前端 未结 3 936
感动是毒
感动是毒 2021-02-20 01:59

Basically I\'m asking the same question as this guy: How to do relative imports in Python?

But no one gave him a correct answer. Given that you are inside a subfolder an

3条回答
  •  面向向阳花
    2021-02-20 02:26

    If you are then importing Module_B in to App, you would

    Module_B.py: import ModuleA

    App.py (which also imports ModuleA which is now by default in your Pythonpath)

    import Module_B.Module_B
    

    Another alternative, is to update __init__.py (the one in Module_A/App folder) to:

    import os
    import sys
    sys.path.extend('%s../' % os.getcwd())
    import ModuleA
    

    Another alternative, is to add your folder to the PYTHONPATH environment var.

提交回复
热议问题