模块类似于其他语言的引入。包是一个文件下有个__init__.py文件称为包。

第九课--python模块与包_自定义

类似与图片这个样子。hellos文件是一个包。

看案例。

hello.py

def hello():
return 'hellos'

16_包.py

from hellos import hello

a = hello.hello()
print(a)

上面是包的使用。包的使用也是引入,但是他是一个文件夹下的文件。

接下来看模块的使用。模块在python中使用的频率很多,如os,math等等。

但是自定义的模块就是一个文件,这个文件不能和python内置的模块名称重合。

如下使用:

support.py(这是模块)

def print_func():
return 'Hello'

16_模块.py(使用模块)

import support

a = support.print_func()
print(a)