模块小结

1.import 模块名(文件)
    导入模块
2.查看模块内的方法
    dir(模块名)
3.if __name__ == "__main__":
    需要测试的语句
4.from 模块名 import 方法/属性/* 
    再使用时 使用 方法即可 不再使用模块名.方法
5.使用 __all__ = ["方法1","方法2","属性名1","属性名2"]
    在导入 * 时,只导入该 all 中的方法或属性
    在 __init__.py 文件第一行
6.import 模块名(文件) as 模块别名
7.搜索模块:
    import sys
    sys.path.append(r'绝对路径')
        绝对路径指 导入的模块的 文件夹的位置

a.py 程序:

import linecache
print(linecache.__file__)
def show( ):
    print("我是 0411 的模块")

程序:
import linecache
# 导入模块
print(dir(linecache))
# 查看都具有哪些方法
'''
['__all__', '__builtins__', '__cached__', 
'__doc__', '__file__', '__loader__', '__name__', 
'__package__', '__spec__', 'cache', 'checkcache', 
'clearcache', 'functools', 'getline', 'getlines', 
'lazycache', 'os', 'sys', 'tokenize', 'updatecache']
'''
linecache.__file__
# 查看模块地址
# F:\Python IDLE\lib\linecache.py
from math import pi 

import sys
sys.path.append(r'D:\见解\Python\Python代码\学习\0411')
if __name__ == "__main__":
    # 执行文件时,进行测试
    print(linecache.__file__)
    # F:\Python IDLE\lib\linecache.py
    
    print(pi)
    # 使用 math 中的 pi 属性
    # 3.141592653589793
    import a
    # 导入 0411 的 a.py 文件
    a.show()
    # 我是 0411 的模块

2020-04-12

posted @ 2020-04-12 11:43  CodeYaSuo  阅读(108)  评论(0编辑  收藏  举报