How to do relative imports in Python?

前端 未结 15 2814
情深已故
情深已故 2020-11-21 04:47

Imagine this directory structure:

app/
   __init__.py
   sub1/
      __init__.py
      mod1.py
   sub2/
      __init__.py
      mod2.py

I\'

15条回答
  •  野的像风
    2020-11-21 05:24

    I found it's more easy to set "PYTHONPATH" enviroment variable to the top folder:

    bash$ export PYTHONPATH=/PATH/TO/APP
    

    then:

    import sub1.func1
    #...more import
    

    of course, PYTHONPATH is "global", but it didn't raise trouble for me yet.

提交回复
热议问题