Python关于import的实验(2) 子包嵌套下的import和from...import...

建立如图的文件夹结构:
在这里插入图片描述

在这里插入图片描述
文件sound/effects/echo.py下的代码:

info = r'我是sound\effects\echo.py'
if __name__ == "__main__":
    pass

控制台下的演示:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS 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.effects.echo
>>> sound.effects.echo.info
'我是sound\\effects\\echo.py'
>>> ^Z

PS 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
>>> echo.info
'我是sound\\effects\\echo.py'
>>> ^Z

PS 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
>>> info
'我是sound\\effects\\echo.py'
>>> 
>>> ^Z

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

当使用 import item.subitem.subsubitem 这样的语法时,除了最后一项之外的每一项都必须是一个包;最后一项可以是模块或包,但不能是前一项中定义的类或函数或变量。控制台中的演示效果如下:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS 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.effects.echo
>>> sound.effects.echo.info
'我是sound\\effects\\echo.py'
>>>
>>> ^Z

PS 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.effects.echo.info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sound.effects.echo.info'; 'sound.effects.echo' is not a package
>>> 
>>> ^Z

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

总结来说,就是import语句只能导入模块或者包,但是不能导入变量或者函数和类。经过试验发现无论是导入模块还是导入包(可以理解为包含python文件的文件夹),python都会认为是导入模块Module,如果你导入了包,后续再引用其子包或者模块的时候会报错,比如:AttributeError: module 'sound.effects' has no attribute 'echo'
用一句话概括,使用import XXX.XXX.XXX语句只能导入模块,除此之外都不能导入或者导入后不能正确使用.(即导入包后无法正常使用,对于函数、类、变量等这些都无法导入.)

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.effects.echo
>>> sound.effects.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.
>>> import sound.effects
>>> sound.effects.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>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
>>> sound.effects.echo.info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'sound' has no attribute 'effects'
>>>
>>> ^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.effects.echo as e
>>> 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.
>>> import sound.effects as ef
>>> ef.echo.info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'sound.effects' has no attribute 'echo'
>>> ef.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.
>>> import sound as snd
>>> snd.effects.echo.info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'sound' has no attribute 'effects'
>>> snd.effects
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'sound' has no attribute 'effects'
>>>
>>> ^Z


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、付费专栏及课程。

余额充值