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'
>>>
>>>
>>>
配套源代码: 源代码及相关文件下载