How to do relative imports in Python?

前端 未结 15 2859
情深已故
情深已故 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:32

    This is solved 100%:

    • app/
      • main.py
    • settings/
      • local_setings.py

    Import settings/local_setting.py in app/main.py:

    main.py:

    import sys
    sys.path.insert(0, "../settings")
    
    
    try:
        from local_settings import *
    except ImportError:
        print('No Import')
    

提交回复
热议问题