python import

时间:2024-04-27 02:13:59

在执行 import module 时 会从

1 当前目录

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

3 python 安装目录

 

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

test.py

 __all__ = ['a', 'b']

 def a():
print "a" def b():
print "b" def c():
print "c"

test_2.py

 from test import *

 def a_2():
print "a_2",
a()

test_3.py

 from test_2 import *

 a()
a_2()

运行 test_3.py

# a

# a_2 a

__all__ 的用法

test.py

 __all__ = ['a', 'b']

 def a():
print "a" def b():
print "b" def c():
print "c"

test_2.py

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