Python 模块(module)

模块(module)也是为了同样的目的。在Python中,一个.py文件就构成一个模块。通过模块,你可以调用其它文件中的程序。

first.py

def laugh():
    print "Haha"

second.py

import first
first.laugh()
import first as f
f.laugh()
from first import laugh
laugh()
from first import *
laugh()

多层引入,加上__init__.py
util目录下有一个util.py

def sayHello():
    print "Hello"

还有一个空的文件__init__.py

使用的时候

import util.util
util.util.sayHello()
from util.util import *
sayHello()

原文地址:https://www.cnblogs.com/jiqing9006/p/10604545.html