脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python3 模块、包调用&路径详解

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

2020-12-13 00:28Infi_chu Python

下面小编就为大家带来一篇Python3 模块、包调用&路径详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'''
以下代码均为讲解,不能实际操作
'''
'''
博客园 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 模块、包调用&路径详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/Infi-chu/archive/2017/10/24/7726319.html

延伸 · 阅读

精彩推荐