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()里。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值