How to accomplish relative import in python

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

s

6条回答
  •  -上瘾入骨i
    2020-12-01 11:21

    From the PEP it appears that you cannot use a relative import to import a file that is not packaged.

    So you would need to add a __init__.py to stuff and change your imports to something like from .mylib import *

    However, the PEP seems to make no allowance to keep mylib packaged up in a module. So you might be required to change how you call your library functions.

    Another alternative is to move mylib into a subpackage and import it as from .libpackage import mylib

提交回复
热议问题