导入模块的方法
import abc #import 模块名(即py文件名abc.py)
abc.abc1()
import abc as a #import 模块名 as 别名
a.abc1()
from abc import abc1 #from 模块名 import 函数 !导入的函数名易于文档函数or属性名重复
abc1()
使用sys.path可查询py导入模块的路径
1>>>import sys
2>>> sys.path
[’’, ‘D:\Python34\Lib\idlelib’, ‘D:\Python34\lib\site-packages\pip-19.0.3-py3.4.egg’, ‘C:\WINDOWS\SYSTEM32\python34.zip’, ‘D:\Python34\DLLs’, ‘D:\Python34\lib’, ‘D:\Python34’, ‘D:\Python34\lib\site-packages’]
sys.path.append(“路径”)添加新的导入模块路径
1>>> sys.path.append(‘D:\Python34\me’)
2>>> sys.path
[’’, ‘D:\Python34\Lib\idlelib’, ‘D:\Python34\lib\site-packages\pip-19.0.3-py3.4.egg’, ‘C:\WINDOWS\SYSTEM32\python34.zip’, ‘D:\Python34\DLLs’, ‘D:\Python34\lib’, ‘D:\Python34’, ‘D:\Python34\lib\site-packages’, ‘D:\Python34\me’]
创建包
1.新建一个文件夹,文件夹的名称 就是 新建包的包名;
2.在该文件夹中,创建一个 __init__.py 文件(前后各有 2 个下划线‘_’),内容可以为空。
插入包
import bag.abc #import 包名.模块名
模块相关
>>> import timeit
>>> dir(timeit) # 获得当前模块的属性列表
['Timer', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_template_func', 'default_number', 'default_repeat', 'default_timer', 'dummy_src_name', 'gc', 'itertools', 'main', 'reindent', 'repeat', 'sys', 'template', 'time', 'timeit']
>>> timeit.__all__ #模块对外提供的接口
['Timer', 'timeit', 'repeat', 'default_timer']
>>> timeit.__file__ #模块的py文件路径
'D:\\Python34\\lib\\timeit.py'
>>> help(time) #模块的用法
Help on built-in module time:
……………………………………………………………………………………………………………………………………………………………………
模块调用路径
import os,sys
a=os.path.abspath(__file__) #当前文件的绝对路径
b=os.path.dirname(a) #a的上级目录sys.
path.append(b+'abc') #添加py模块调用路径