__init__.py

在目录内增加一个 __init__.py使得该目录可以被当作模块导入
init_test/
      |___ test/
      |         |__  __init__.py
      |         |__  test.py
      |___ test.py

#init_test/test/__init__.py
# -*- coding: utf-8 -*-
print("in test/__init__.py")

#init_test/test/test.py
# -*- coding: utf-8 -*-
print("in test/test.py")
print("module name:",__name__);
def test_hello():
    print("hello from test.test.test_hello");

--------------------测试程序1----------------------------
#init_test/test.py
# -*- coding: utf-8 -*-
import test
import test.test
test.test.test_hello()

#程序运行结果:
in test/__init__.py
in test/test.py
module name: test.test
hello from test.test.test_hello

--------------------测试程序2----------------------------
#init_test/test.py
# -*- coding: utf-8 –*-
#import test
import test.test
test.test.test_hello()

#程序运行结果:
in test/__init__.py
in test/test.py
module name: test.test
hello from test.test.test_hello

--------------------测试程序3----------------------------
#init_test/test.py
# -*- coding: utf-8 -*-
import test               #
#import test.test
#test.test.test_hello()

#程序运行结果:
in test/__init__.py

__init__.py 使得该文件可以被当作模块导入
import test  #只运行 test/__init__.py

import test.test  #如果第一次导入 test 内的文件,则先运行 test/__init__.py,再运行 test/test.py;否则只运行 test/test.py

你可能感兴趣的:(Module,测试,import)