Python - Doing absolute imports from a subfolder

前端 未结 3 2400
鱼传尺愫
鱼传尺愫 2021-02-20 01:56

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:39

    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.

提交回复
热议问题