Circular (or cyclic) imports in Python

来源:互联网 发布:淘宝充王者荣耀点券 编辑:程序博客网 时间:2024/05/16 10:38

Circular (or cyclic) imports in Python

up vote73down votefavorite
25

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.LottApr 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

activeoldestvotes
up vote81down voteaccepted

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
up vote61down vote

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
 

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