Importing modules from parent folder

前端 未结 22 3118
梦毁少年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条回答
  •  执笔经年
    2020-11-21 23:48

    The pathlib library (included with >= Python 3.4) makes it very concise and intuitive to append the path of the parent directory to the PYTHONPATH:

    import sys
    from pathlib import Path
    sys.path.append(str(Path('.').absolute().parent))
    

提交回复
热议问题