Chinaunix首页 | 论坛 | 博客
  • 博客访问: 327016
  • 博文数量: 81
  • 博客积分: 1810
  • 博客等级: 上尉
  • 技术积分: 725
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-25 17:38
文章分类

全部博文(81)

文章存档

2016年(4)

2015年(11)

2014年(16)

2013年(37)

2012年(11)

2011年(2)

我的朋友

分类: Python/Ruby

2016-04-20 22:39:50

一, 先讲一下基本原理,和基本的解决方法 



20
down voteaccepted

Modules can import each other cyclically, but there's a catch. In the simple case, it should work by moving the import statements to the bottom of the file or not using the from syntax.

Here's why that works:

When you import a module, Python first checks sys.modules. If it's in there, it just imports from there. If it's not there, it tries to import it in the normal way; basically, it finds the file and runs the stuff in it.

Running a module populates the module's contents. For example, say we have this module, creatively named example_opener:

import webbrowser

def open_example():
    webbrowser.open('') 

At the start, the module is empty. Then Python executes:

import webbrowser 

After that, the module only contains webbrowser. Then Python executes this:

def open_example():
    webbrowser.open('') 

Python creates open_example. Now the module contains webbrowser and open_example.

Say webbrowser contained this code:

from example_opener import open_example

def open(url):
    print url 

Say example_opener is imported first. This code is executed:

import webbrowser 

webbrowser has not yet been imported, so Python executes the contents of webbrowser:

from example_opener import open_example 

example_opener has been imported, but not yet fully executed. Python doesn't care. Python pulls the module out of sys.modules. At this point, example_opener is still empty. It hasn't defined open_example yet, nor even completed importing webbrowser. Python can't find open_example in example_opener, so it fails.

What if we imported open_example from the end of webbrowser and webbrowser from the end of example_opener? Python would start by executing this code:

def open_example():
    webbrowser.open('') 

webbrowser does not exist yet, but it doesn't matter until open_example is called. Now example_opener contains only open_example. It then executes:

import webbrowser 

It has not been imported yet, so Python executes webbrowser. It starts:

def open(url):
    print url 

It defines open. Then it executes:

from example_opener import open_example 

example_opener is in sys.modules, so it uses that. example_opener contains open_example, so it succeeds. Python finishes importing webbrowser. That concludes importing webbrowser from example_opener. That's the last thing in example_opener, so the import of example_opener finishes, successful, as well.

shareimprove this answer
    
Nice breakdown. This is usually what I have to do in django where sometimes you just have to import circularly. Import at the bottom or within the function or method scope. – jdi Jul 28 '12 at 5:39 
    
Added a paragraph at the bottom and marked this correct – Hubro Jul 28 '12 at 5:54
    
@Codemonkey: I noticed you added that you can avoid using the from ... import ... syntax, but I already have that in my answer at the top: "In the simple case, it should work by [...] not using the fromsyntax." – icktoofay Jul 28 '12 at 5:54 
    
Wow, didn't even notice – Hubro Jul 28 '12 at 6:10 
二,flask给出的flask项目结构


三,看看这个项目怎么实践的
https://github.com/tquach/talent-curator

四,Circular Import真是恶心,改改改!也可能是我姿势不对,请指正!

阅读(1491) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~