Python关于import的实验(3) __init__.py的作用

在这里插入图片描述

在导入包的时候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>

配套源代码: 源代码及相关文件下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值