Circular (or cyclic) imports in Python
来源:互联网 发布:淘宝充王者荣耀点券 编辑:程序博客网 时间:2024/05/16 10:38
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?
4 Answers
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.
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
- from b import getb
- print '------init a-------'
- def hello():
- print getb()
b.py
- from a import hello
- print '------init b-------'
- def getb():
- return "i am b"
- def my_hello():
- hello()
执行a.py 会报错:ImportError: cannot import name getb
解决办法,修改a.py,把
- from b import getb
移到 hello()里。
- Circular (or cyclic) imports in Python
- 关于python中模块的环状引用(circular imports)
- 关于python模块循环导入(circular imports)/相对导入(Relative Imports)/绝对导入(Absolute Imports)的官方资料
- python in or of opencv
- Reference Or Value pass in python
- [python] Adding or Deleting elements in a list or set
- cyclic
- 【并行计算】Circular-Shift Routing in Hypercube
- 【Python基础】之has_key(key) or key in d
- Imports(namespace)
- Declaring Imports
- Relative imports
- HTML Imports
- HTML Imports
- spring:Circular placeholder reference 'XXX' in property definitions
- windows phone:Solving Circular Navigation in Windows Phone Silverlight Applications
- spring:Circular placeholder reference 'XXX' in property definitions
- Circular dependencies cannot exist in RelativeLayout错误解决
- 图像处理Matlab(二)——生成随机点
- ToInterface
- 用实体关系图进行数据库建模
- 内核统计到应用占用内存的解读
- .输出数组中下标为3的数。和值为3的倍数的元素
- Circular (or cyclic) imports in Python
- c++引用详解
- javascript日期操作
- linux学习笔记
- lighttpd基础配置
- android 解析json数据格式
- bellman-ford算法的优化spfa算法
- win7安装IIS及将网站发布到IIS上
- ubuntu 下安装eclipse,并搭建c++/c开发环境