Circular (or cyclic) imports in Python

What will happen if two modules import each other?

To generalize the problem, what about the cyclic imports in Python?

share | improve this question
 
2  
Good question; I believe the terminology is "circular imports" not "cyclic", though. I would imagine it'd be best if you change your title so other people can find this later on. –   Paolo Bergantino  Apr 13 '09 at 16:18
4  
@Paolo I think it's cyclic .. not circular –   hasenj  Apr 13 '09 at 16:22
 
@hasen j: I wasn't sure which is why I didn't edit the question, but a google search provided a lot of articles that refer to it as circular, so I figured that was the term used. –   Paolo Bergantino  Apr 13 '09 at 16:36
1  
@hansen j: I vote for "circular". Cyclic implies there's a loop -- or something -- which cycles around doing imports more than once. This is mutual references, which is most commonly described as circular. –   S.Lott Apr 13 '09 at 16:50
5  
Well in graphs we call it cyclic, if a cycle exists. –   Xolve  Apr 14 '09 at 9:08
show 3 more comments

4 Answers

up vote 81 down vote accepted

There was a really good discussion on this over at comp.lang.python last year. It answers your question pretty thoroughly.

Imports are pretty straightforward really. Just remember the following:

'import' and 'from xxx import yyy' are executable statements. They execute when the running program reaches that line.

If a module is not in sys.modules, then an import creates the new module entry in sys.modules and then executes the code in the module. It does not return control to the calling module until the execution has completed.

If a module does exist in sys.modules then an import simply returns that module whether or not it has completed executing. That is the reason why cyclic imports may return modules which appear to be partly empty.

Finally, the executing script runs in a module named __main__, importing the script under its own name will create a new module unrelated to __main__.

Take that lot together and you shouldn't get any surprises when importing modules.

share | improve this answer
 
4  
This is fairly a great answer. –   Xolve  Apr 14 '09 at 9:09
 
@Shane, all ... what if my circular dependency go deeper. What if within xxx I've a module level variable (xxx.var) - which I want to import at module yyy: from xxx import var. This creates an importError exception for me - and according to your description that should be the case. When the interpreter see the line 'from yyy import var', the variable var is not yet defined. What can I do in this case? –   Uri  Jun 19 at 16:25
add comment

If you do "import foo" inside bar and "import bar" inside foo, it will work fine. By the time anything actually runs, both modules will be fully loaded and will have references to each other.

The problem is when instead you do "from foo import abc" and "from bar import xyz". Because now each module requires the other mod


python 循环依赖问题

 

发现python比java更容易出现循环依赖的问题


manager模块里定义了一个方法入口比如 get_by_tag(),通过search来实现,但search需要依赖models获取一些信息,这就产生了一个循环以来,可以通过两端代码来测试:

 

a.py

A.py代码   收藏代码
  1. from b import getb  
  2.   
  3. print '------init a-------'  
  4. def hello():  
  5.   
  6.     print getb()  

 b.py

Python代码   收藏代码
  1. from a import hello  
  2. print '------init b-------'  
  3.   
  4. def getb():  
  5.     return "i am b"  
  6.   
  7. def my_hello():  
  8.     hello()  

 

执行a.py 会报错:ImportError: cannot import name getb

 

解决办法,修改a.py,把

 

A.py代码   收藏代码
  1. from b import getb  

 

移到 hello()里。


内容概要:《学术研究提示设计 50 招》是一份详尽的指南,旨在帮助研究人员提高学术写作和研究效率。该文档涵盖了从论文撰写、润色、翻译、查重降重、参考文献管理、投稿审稿到文献阅读等多个方面的具体操作指令。每一章节均针对特定任务提供了详细的步骤和注意事项,例如如何撰写标题、摘要、致谢,如何进行英文润色、中英翻译,以及如何优化逻辑结构等。文档还介绍了如何利用AI工具进行文献分析、术语表提取和研究方向探索等内容,为研究者提供了全面的支持。 适合人群:适用于学术研究人员,特别是那些需要撰写、润色和提交学术论文的研究者,包括研究生、博士生及高校教师等。 使用场景及目标:① 提供一系列具体的指令,帮助研究者高效完成论文的各个部分,如撰写标题、摘要、致谢等;② 提供润色和翻译的详细指导,确保论文语言的准确性和专业性;③ 提供查重降重的方法,确保论文的原创性;④ 提供参考文献管理和投稿审稿的指导,帮助研究者顺利发表论文;⑤ 利用AI工具进行文献分析、术语表提取和研究方向探索,提高研究效率。 阅读建议:此资源不仅提供了具体的指令和方法,更重要的是引导研究者如何思考和解决问题。因此,在学习过程中,不仅要关注具体的步骤,还要理解背后的原理和逻辑,结合实际案例进行实践和反思。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值