Importing modules from parent folder

前端 未结 22 2732
梦毁少年i
梦毁少年i 2020-11-21 23:19

I am running Python 2.5.

This is my folder tree:

ptdraft/
  nib.py
  simulations/
    life/
      life.py

(I also have __init

22条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 23:31

    You can use OS depending path in "module search path" which is listed in sys.path . So you can easily add parent directory like following

    import sys
    sys.path.insert(0,'..')
    

    If you want to add parent-parent directory,

    sys.path.insert(0,'../..')
    

    This works both in python 2 and 3.

提交回复
热议问题