python import

在执行 import module 时 会从

1 当前目录

2 pythonpath(可以通过 os.sys.path 查看)

3 python 安装目录

 

b import 了 a, c import 了 b,c 中也会有定义

test.py

 1 __all__ = ['a', 'b']
 2 
 3 def a():
 4     print "a"
 5 
 6 def b():
 7     print "b"
 8 
 9 def c():
10     print "c"

test_2.py

1 from test import *
2 
3 def a_2():
4     print "a_2",
5     a()

test_3.py

1 from test_2 import *
2 
3 a()
4 a_2()

运行 test_3.py

# a

# a_2 a

__all__ 的用法

test.py

 1 __all__ = ['a', 'b']
 2 
 3 def a():
 4     print "a"
 5 
 6 def b():
 7     print "b"
 8 
 9 def c():
10     print "c"

test_2.py

1 from test import *
2 
3 a()
4 # c() cause error

 

posted @ 2014-09-10 09:20  mess4u  阅读(157)  评论(0编辑  收藏  举报