栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在python中完成相对导入

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在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
)移到该程序包中,一切就变得很笨拙了。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617796.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号