Python3 模块、包调用&路径详解 如下所示: ''' 以下代码均为讲解,不能实际操作 ''' ''' 博客园 Infi_chu ''' ''' 模块的优点: 1.高可维护性 2.可以大大减少编写的代码量 模块一共有三种: 1.Python标准库 2.第三方模块 3.应用程序自定义模块 ''' # import example # 调用example模块 # from example import example # 调用example模块中的一个example方法 ''' 博客园 Infi_chu ''' ''' 包的特点: 1.有__init__.py文件 2.有很多模块组成 ''' # from test import example # test为包名,example模块名,一层嵌套 # from test.test1 import example # test1、test2均为包名,test1在test中,example为模块名 # from test.test1.func1 import example # func1是example模块中的一个func1方法 # import test # test为包名,此命令相当于执行了__init__文件 ''' 博客园 Infi_chu ''' ''' import 包 或 模块 的区别 import 包 只是执行了一个__init__.py文件,并没有与其他模块产生联系,取值时需要加.调用 import 模块 是直接调用模块 ''' ''' 路径解决 ''' # import sys,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 模块、包调用&路径详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持中文源码网。