Importing modules from parent folder

前端 未结 22 2676
梦毁少年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:52

    I found the following way works for importing a package from the script's parent directory. In the example, I would like to import functions in env.py from app.db package.

    .
    └── my_application
        └── alembic
            └── env.py
        └── app
            ├── __init__.py
            └── db
    
    import os
    import sys
    currentdir = os.path.dirname(os.path.realpath(__file__))
    parentdir = os.path.dirname(currentdir)
    sys.path.append(parentdir)
    

提交回复
热议问题