Python关于import的实验(4) - from package import *

在这里插入图片描述
import 语句使用下面的规范:如果一个包的 __init__.py 代码定义了一个名为 __all__ 的列表,它会被视为在遇到 from package import * 时应该导入的模块名列表。在发布该包的新版本时,包作者可以决定是否让此列表保持更新。包作者如果认为从他们的包中导入 * 的操作没有必要被使用,也可以决定不支持此列表。例如,文件 sound/effects/__init__.py 可以包含以下代码:

__all__ = ["echo", "surround"] # __all__ = ["echo", "surround", "reverse"]

控制台下的操作:

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 import *
初始化...我是__init__文件:sound\__init__.py
>>> echo
<module 'sound.effects.echo' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\effects\\echo.py'>
>>> surround
<module 'sound.effects.surround' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\effects\\surround.py'>
>>> reverse
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'reverse' is not defined
>>>
>>> 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.filters import *
初始化...我是__init__文件:sound\__init__.py
>>>
>>> equalizer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'equalizer' is not defined
>>> karaoke
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'karaoke' is not defined
>>> vocoder
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'vocoder' is not defined
>>> from sound.filters import equalizer,karaoke,vocoder
>>> equalizer
<module 'sound.filters.equalizer' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\filters\\equalizer.py'>
>>> karaoke
<module 'sound.filters.karaoke' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\filters\\karaoke.py'>
>>> vocoder
<module 'sound.filters.vocoder' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\filters\\vocoder.py'>
>>>
>>>
>>> ^Z


C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>

在根目录下添加几个python文件:
在这里插入图片描述
调整sound/__init__.py文件的代码:

__all__ = ["aaa",'bbb','ccc','ddd','ZZZ']
# __all__ = ["aaa",'bbb','ccc','ddd']
print(r"初始化...我是__init__文件:sound\__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
>>>
>>> ^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 *
初始化...我是__init__文件:sound\__init__.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'sound' has no attribute 'ZZZ'
>>>
>>> ^Z


C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
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 import *
初始化...我是__init__文件:sound\__init__.py
>>>
>>>
>>> ^Z


C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>


报错的原因是我们没有ZZZ.py文件,我们可以加上。
在这里插入图片描述
继续实验,再修改sound/__init__.py文件内容如下:

__all__ = ["aaa",'bbb','ccc','ddd','ZZZ',"filters","effects"]
# __all__ = ["aaa",'bbb','ccc','ddd','ZZZ']
# __all__ = ["aaa",'bbb','ccc','ddd']
print(r"初始化...我是__init__文件:sound\__init__.py")

文件夹不改动:
在这里插入图片描述
分别在sound/__init__.py文件代码改动前后在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.
>>> import sound
初始化...我是__init__文件:sound\__init__.py
>>> from sound import *
>>>
>>> aaa
<module 'sound.aaa' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\aaa.py'>
>>> ZZZ
<module 'sound.ZZZ' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\ZZZ.py'>
>>>
>>> filters
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'filters' is not defined
>>> ^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 *
初始化...我是__init__文件:sound\__init__.py
>>>
>>> aaa
<module 'sound.aaa' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\aaa.py'>
>>> ZZZ
<module 'sound.ZZZ' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\ZZZ.py'>
>>> ddd
<module 'sound.ddd' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\ddd.py'>
>>> bbb
<module 'sound.bbb' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\bbb.py'>
>>> ccc
<module 'sound.ccc' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\ccc.py'>
>>> filters
<module 'sound.filters' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\filters\\__init__.py'>
>>> formats
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'formats' is not defined
>>> effects
<module 'sound.effects' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\effects\\__init__.py'>
>>>
>>>
>>> ^Z


C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
C:\Users\chenxuqi\Desktop\新建文件夹\testImport>
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 import *
初始化...我是__init__文件:sound\__init__.py
>>> filters
<module 'sound.filters' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\testImport\\sound\\filters\\__init__.py'>
>>> filters.__init__
<method-wrapper '__init__' of module object at 0x0000028C453343B8>
>>> filters.vocoder
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'sound.filters' has no attribute 'vocoder'
>>> filters.karaoke
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'sound.filters' has no attribute 'karaoke'
>>> filters.equalizer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'sound.filters' has no attribute 'equalizer'
>>>
>>>
>>>

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值