pythoninit文件_从__init__.py导入Python文件

Suppose I have the following structure:

app/

__init__.py

foo/

a.py

b.py

c.py

__init__.py

a.py, b.py and c.py share some common imports (logging, os, re, etc). Is it possible to import these three or four common modules from the __init__.py file so I don't have to import them in every one of the files?

Edit: My goal is to avoid having to import 5-6 modules in each file and it's not related to performance reasons.

解决方案

No, they have to be put in each module's namespace, so you have to import them somehow (unless you pass logging around as a function argument, which would be a weird way to do things, to say the least).

But the modules are only imported once anyway (and then put into the a, b, and c namespaces), so don't worry about using too much memory or something like that.

You can of course put them into a separate module and import that into each a, b, and c, but this separate module would still have to be imported everytime.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值