How to accomplish relative import in python

前端 未结 6 1256
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 10:38
stuff/
    __init__.py
    mylib.py
    Foo/
        __init__.py
        main.py
        foo/
            __init__.py
            script.py

s

6条回答
  •  不知归路
    2020-12-01 11:13

    though as long "stuff" is not in your python PATH you got no choice than adding the path.

    If you know the level of your script.py from stuff you can do for example:

    import sys
    import os
    sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
    

提交回复
热议问题