Python3 模块包调用 Python3 模块、包调用&路径详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python3 模块包调用 Python3 模块、包调用&路径详解

Infi_chu   2021-03-28 我要评论
想了解Python3 模块、包调用&路径详解的相关内容吗,Infi_chu在本文为您仔细讲解Python3 模块包调用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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 模块、包调用&路径详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们