Python官方文档参考链接: 常规包
Python 定义了两种类型的包,常规包 和 命名空间包。 常规包是传统的包类型,它们在 Python 3.2 及之前就已存在。 常规包通常以一个包含 __init__.py 文件的目录形式实现。 当一个常规包被导入时,这个 __init__.py 文件会隐式地被执行,它所定义的对象会被绑定到该包命名空间中的名称。__init__.py 文件可以包含与任何其他模块中所包含的 Python 代码相似的代码,Python 将在模块被导入时为其添加额外的属性。 例如,以下文件系统布局定义了一个最高层级的 parent 包和三个子包. 导入 parent.one 将隐式地执行 parent/__init__.py 和 parent/one/__init__.py。 后续导入 parent.two 或 parent.three 则将分别执行 parent/two/__init__.py 和 parent/three/__init__.py。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN2XjlGcjAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLz0EROpXVq5kMRpHW3BjMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL1QjMxQDNwMjMwITMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
parent\__init__.py:
print("导入parent包...内部已经导入random库")
str_parent = r"我是parent\__init__.py里的字符串..."
import random
parent\two\__init__.py:
print("导入parent/two包...内部已经导入time库")
str_parent_two = r"我是parent\two\__init__.py里的字符串..."
import time
parent\three\__init__.py:
print("导入parent/three包...内部已经导入jieba库")
str_parent_three = r"我是parent\three\__init__.py里的字符串..."
import jieba
parent\one\__init__.py:
print("导入parent/one包...内部已经导入hashlib库")
str_parent_one = r"我是parent\one\__init__.py里的字符串..."
import hashlib
cmd控制台下的操作:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
PS C:\Users\chenxuqi\Desktop\新建文件夹\测试包> python
Type "help", "copyright", "credits" or "license" for more information.
>>> import parent
导入parent包...内部已经导入random库
>>> parent.str_parent
'我是parent\\__init__.py里的字符串...'
>>> random
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'random' is not defined
>>> parent.random
<module 'random' from 'D:\\Python\\Python37\\lib\\random.py'>
>>> parent.random.randint(2,7)
7
>>> parent.random.randint(2,7)
7
>>> parent.random.randint(2,7)
7
>>> parent.random.randint(2,7)
7
>>> parent.random.randint(2,7)
7
>>> parent.random.randint(2,9)
5
>>> parent.random.randint(2,9)
2
>>> parent.random.randint(2,9)
3
>>> parent.random.randint(2,9)
8
>>> parent.random.randint(2,7)
5
>>> parent.random.randint(2,7)
2
>>> parent.random.randint(2,7)
5
>>> parent.random.randint(2,7)
6
>>> import parent.two
导入parent/two包...内部已经导入time库
>>> parent.two.str_parent_two
'我是parent\\two\\__init__.py里的字符串...'
>>> parent.two.time
<module 'time' (built-in)>
>>> parent.two.time.time()
1605883430.647523
>>> time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'time' is not defined
>>> from parent.two import time
>>> time
<module 'time' (built-in)>
>>> now = time.time()
>>> print(time.ctime(now))
Fri Nov 20 22:46:45 2020
>>> import parent.three
导入parent/three包...内部已经导入jieba库
>>> import parent.three as three
>>> three.str_parent_three
'我是parent\\three\\__init__.py里的字符串...'
>>> three.jieba
<module 'jieba' from 'D:\\Python\\Python37\\lib\\site-packages\\jieba\\__init__.py'>
>>> jieba.lcut("全国计算机等级考试Python科目")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'jieba' is not defined
>>> three.jieba.lcut("全国计算机等级考试Python科目")
Building prefix dict from the default dictionary ...
Dumping model to file cache C:\Users\chenxuqi\AppData\Local\Temp\jieba.cache
Loading model cost 0.857 seconds.
Prefix dict has been built successfully.
['全国', '计算机', '等级', '考试', 'Python', '科目']
>>> from parent.one import str_parent_one
导入parent/one包...内部已经导入hashlib库
>>> str_parent_one
'我是parent\\one\\__init__.py里的字符串...'
>>> from parent.one import hashlib as hsb
>>> hsb
<module 'hashlib' from 'D:\\Python\\Python37\\lib\\hashlib.py'>
>>> hsb.md5("尹增宝爱听古风音乐".encode("utf8")).hexdigest()
'075649873523f1e621d4b33f69b60ebb'
>>>
>>> hsb.md5("林祖泉".encode("utf8")).hexdigest()
'738974139c96e7c1b174e1f886ffb641'
>>>
>>>
导入一个包之后,无法访问其子包,除非其子包有__init__.py文件,而且该文件已经被执行过一次,那么该包就会有其该子包的属性,否则会报错,比如:AttributeError: module 'parent' has no attribute 'three'
修改./__init__.py文件:
print("导入parent包...内部已经导入random库")
str_parent = r"我是parent\__init__.py里的字符串..."
import random
import parent.one as yi
from parent import two
# import parent.three
cmd控制台下的操作如下:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
PS C:\Users\chenxuqi\Desktop\新建文件夹\测试包> python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> parent
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'parent' is not defined
>>> import parent
导入parent包...内部已经导入random库
导入parent/one包...内部已经导入hashlib库
导入parent/two包...内部已经导入time库
>>> parent
<module 'parent' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\测试包\\parent\\__init__.py'>
>>> parent.one
<module 'parent.one' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\测试包\\parent\\one\\__init__.py'>
>>> parent.yi
<module 'parent.one' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\测试包\\parent\\one\\__init__.py'>
>>> parent.two
<module 'parent.two' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\测试包\\parent\\two\\__init__.py'>
>>> yi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'yi' is not defined
>>> two
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'two' is not defined
>>> one
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'one' is not defined
>>> three
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'three' is not defined
>>> parent.three
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'parent' has no attribute 'three'
>>> import parent.three
导入parent/three包...内部已经导入jieba库
>>> parent.three
<module 'parent.three' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\测试包\\parent\\three\\__init__.py'>
>>> three
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'three' is not defined
>>> import parent.three as three
>>> three
<module 'parent.three' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\测试包\\parent\\three\\__init__.py'>
>>>
>>>
实验源代码获取: 源代码下载链接