您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在python中完成相对导入

如何在python中完成相对导入

稍微摆弄一下之后,我意识到了如何设置它,并且出于特定性考虑,我将不使用foo bar名称。我的项目目录设置为…

tools/
    core/
        object_editor/
            # files that need to use ntlib.py
            editor.py # see example at bottom
            __init__.py
        state_editor/
            # files that need to use ntlib.py
            __init__.py
        ntlib.py
        __init__.py # core is the top level package
    LICENSE
    state_editor.py # equivalent to main.py for the state editor
    object_editor.py # equivalent to main.py for the object editor

一条线object_editor.py看起来像…

from core.object_editor import editor

一条线editor.py看起来像…

from .. import ntlib

或者

from core import ntlib

关键是在我给问题的示例中,“主要”脚本是从程序包中运行的。一旦将其移出,创建了一个特定的程序包(core),然后将我希望编辑者共享的库(ntlib)移到该程序包中,一切就变得很笨拙了。

python 2022/1/1 18:49:46 有356人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶