python 学习笔记-module

python module

模块式包含python 定义和语句的文件。 文件名以 .py 结尾的。

定义 发表module, fab.py

##fab.py
def fab(n):
    a,b =0,1
    while b <n:
        print(b, end =" ")
        a,b = b, a+b
    print() 
导入module方式一
### main.py
import fab
if __name__ == "__main__":
    fab.fab(100)
    print(fab.__name__)
运行结果:
$ python3 main.py 
1 1 2 3 5 8 13 21 34 55 89 
fab
导入module方式 二
import fab as fb
if __name__ == "__main__":
    fb.fab(100)
    print(fb.__name__)
$ python3 main.py 
1 1 2 3 5 8 13 21 34 55 89 
fab
导入module 方式三
from fab import *
if __name__ == "__main__":
    fab(100
$ python3 main.py 
1 1 2 3 5 8 13 21 34 55 89 
module path 的搜素顺序

1, 先从内存中查找
2,从内置,模块里找
3,从 sys.path 环境变量中搜索
4,从标准库中 sys。module 中搜索

from fab import *
import sys
if __name__ == "__main__":
    fab(100)
    print(sys.builtin_module_names)
    print(sys.path)
    print(sys.modules.keys())
 python3 main.py 
1 1 2 3 5 8 13 21 34 55 89 
('_ast', '_codecs', '_collections', '_functools', '_imp', '_io', '_locale', '_operator', '_signal', '_sre', '_stat', '_string', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', 'atexit', 'builtins', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'posix', 'pwd', 'sys', 'time', 'xxsubtype', 'zipimport')
dict_keys(['builtins', 'sys', '_frozen_importlib', '_imp', '_warnings', '_thread', '_weakref', '_frozen_importlib_external', '_io', 'marshal', 'posix', 'zipimport', 'encodings', 'codecs', '_codecs', 'encodings.aliases', 'encodings.utf_8', '_signal', '__main__', 'encodings.latin_1', 'io', 'abc', '_weakrefset', 'site', 'os', 'errno', 'stat', '_stat', 'posixpath', 'genericpath', 'os.path', '_collections_abc', '_sitebuiltins', 'sysconfig', '_sysconfigdata_m_linux_x86_64-linux-gnu', '_bootlocale', '_locale', 'abrt_exception_handler3', 'fab'])
['/home/lily/python', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']
[lily@localhost python]$ python3 main.py 
1 1 2 3 5 8 13 21 34 55 89 
('_ast', '_codecs', '_collections', '_functools', '_imp', '_io', '_locale', '_operator', '_signal', '_sre', '_stat', '_string', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', 'atexit', 'builtins', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'posix', 'pwd', 'sys', 'time', 'xxsubtype', 'zipimport')

['/home/lxxx/python', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']

dict_keys(['builtins', 'sys', '_frozen_importlib', '_imp', '_warnings', '_thread', '_weakref', '_frozen_importlib_external', '_io', 'marshal', 'posix', 'zipimport', 'encodings', 'codecs', '_codecs', 'encodings.aliases', 'encodings.utf_8', '_signal', '__main__', 'encodings.latin_1', 'io', 'abc', '_weakrefset', 'site', 'os', 'errno', 'stat', '_stat', 'posixpath', 'genericpath', 'os.path', '_collections_abc', '_sitebuiltins', 'sysconfig', '_sysconfigdata_m_linux_x86_64-linux-gnu', '_bootlocale', '_locale', 'abrt_exception_handler3', 'fab'])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值