Python3 模块、包调用&路径详解
如下所示:
''' 以下代码均为讲解,不能实际操作 ''' ''' 博客园Infi_chu ''' ''' 模块的优点: 1.高可维护性 2.可以大大减少编写的代码量 模块一共有三种: 1.Python标准库 2.第三方模块 3.应用程序自定义模块 ''' #importexample#调用example模块 #fromexampleimportexample#调用example模块中的一个example方法 ''' 博客园Infi_chu ''' ''' 包的特点: 1.有__init__.py文件 2.有很多模块组成 ''' #fromtestimportexample#test为包名,example模块名,一层嵌套 #fromtest.test1importexample#test1、test2均为包名,test1在test中,example为模块名 #fromtest.test1.func1importexample#func1是example模块中的一个func1方法 #importtest#test为包名,此命令相当于执行了__init__文件 ''' 博客园Infi_chu ''' ''' import包或模块的区别 import包只是执行了一个__init__.py文件,并没有与其他模块产生联系,取值时需要加.调用 import模块是直接调用模块 ''' ''' 路径解决 ''' #importsys,os #a=os.path.abspath(__file__)#得到绝对路径 #print(a) #print(os.path.dirname(a))#得到上一层路径 #base_dir=os.path.dirname(os.path.dirname(a))#得到上上一层路径 #print(base_dir) #sys.path.append(base_dir) ''' 博客园Infi_chu '''
以上这篇Python3模块、包调用&路径详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。