在导入包的时候Python会自动递归执行包下面的__init__.py文件。注意,只在首次导入的时候执行,并且只执行一次,再次导入的时候不再执行,具体请看如下例子.
修改文件:sound\__init__.py
print(r"初始化...我是__init__文件:sound\formats\__init__.py")
修改文件:sound\formats\__init__.py
print(r"我是文件:sound\formats\__init__.py")
命令行下的操作:
Microsoft Windows [版本 10.0.18363.1198]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>cd /d C:\Users\chenxuqi\Desktop\新建文件夹\testImport
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>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.
>>> import sound
初始化...我是__init__文件:sound\__init__.py
>>> import sound.formats
我是文件:sound\formats\__init__.py
>>>
>>> ^Z
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>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.
>>> import sound.formats
初始化...我是__init__文件:sound\__init__.py
我是文件:sound\formats\__init__.py
>>> import sound
>>>
>>> ^Z
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>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.
>>> import sound.formats.aiffread
初始化...我是__init__文件:sound\__init__.py
我是文件:sound\formats\__init__.py
>>> import sound.formats.aiffread
>>>
>>> import sound.formats
>>> import sound
>>>
>>> ^Z
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>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.
>>> import sound
初始化...我是__init__文件:sound\__init__.py
>>> import sound.formats
我是文件:sound\formats\__init__.py
>>> import sound.formats.aiffread
>>>
>>> ^Z
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
请注意,当使用 from package import item 时,item可以是包的子模块(或子包),也可以是包中定义的其他名称,如函数,类或变量。 import 语句首先测试是否在包中定义了item;如果没有,它假定它是一个模块并尝试加载它。如果找不到它,则引发 ImportError 异常。
通过实验得出如下结论:使用 from package import item 语句时,导入的item可以是子模块、子包、或者函数、类、变量等。但是通过实验发现,如果导入一个包,虽然可以正常导入,但python实际会把这个包当做一个模块,在引用时会报错,比如:AttributeError: module 'sound.effects' has no attribute 'echo 综上所述,from package import item只能导入模块或者导入模块内的函数、变量、类等,虽然能导入包,但是导入的包不能正常引用。'
cmd控制台下的操作演示如下:
Microsoft Windows [版本 10.0.18363.1198]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>cd /d C:\Users\chenxuqi\Desktop\新建文件夹\testImport
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>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.
>>> from sound.effects.echo import info
初始化...我是__init__文件:sound\__init__.py
>>> from sound.effects.echo import info
>>> info
'我是sound\\effects\\echo.py'
>>>
>>> ^Z
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>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.
>>> from sound.effects.echo import info as iinnffoo
初始化...我是__init__文件:sound\__init__.py
>>> iinnffoo
'我是sound\\effects\\echo.py'
>>>
>>> ^Z
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>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.
>>> from sound.effects import echo as e
初始化...我是__init__文件:sound\__init__.py
>>> e.info
'我是sound\\effects\\echo.py'
>>>
>>> ^Z
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>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.
>>> from sound.effects import echo
初始化...我是__init__文件:sound\__init__.py
>>> echo.info
'我是sound\\effects\\echo.py'
>>>
>>> ^Z
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>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.
>>> from sound import effects
初始化...我是__init__文件:sound\__init__.py
>>> effects.echo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'sound.effects' has no attribute 'echo'
>>> ^Z
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>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.
>>> from sound import effects as ef
初始化...我是__init__文件:sound\__init__.py
>>> ef
<module 'sound.effects' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\effects\\__init__.py'>
>>> ef.echo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'sound.effects' has no attribute 'echo'
>>>
>>> ef.echo.info
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'sound.effects' has no attribute 'echo'
>>>
>>>
>>> ^Z
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
配套源代码: 源代码及相关文件下载