Python关于import的实验(6) 在子包中的模块的import语句作为子程序被调用并且也可以作为主程序独立执行,则必须要使用绝对导入

Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.

# 如果一定要作为主程序来执行import语句,
# 而且同时要能被其他程序如test4main.py来调用
# 那么必须使用绝对导入,并且添加sys.path.append(os.getcwd())
# 不能使用相对导入,因为相对导入是基于当前模块的__name__,
# 而主程序的__name__总是'__main__',
# 所以不能使用相对导入,只能使用绝对导入.
# Note that relative imports are based on the 
# name of the current module. Since the name 
# of the main module is always "__main__", 
# modules intended for use as the main module 
# of a Python application must always use absolute imports.

文件夹的布局:
在这里插入图片描述
test4main_1.py

# from sound.filters import test4cxq
from sound.filters import test4cxq_1

sound\filters\test4cxq_1.py



# 绝对导入
import sound.effects.surround # 这行代码被test4main.py正常调用
print(sound.effects.surround.info)
r'''
这部分代码不能直接作为主程序调用,
否则会报错,如下:
Traceback (most recent call last):
  File "c:\Users\chenxuqi\Desktop\新建文件夹\testImport\sound\filters\test4cxq_1.py", 
line 4, in <module>
    import sound.effects.surround # 这行代码被test4main.py正常调用
ModuleNotFoundError: No module named 'sound'
'''



from sound.effects import reverse # 这行代码被test4main.py正常调用 
print(reverse.info)
r'''
这部分代码不能直接作为主程序调用,
否则会报错,如下:
Traceback (most recent call last):
  File "c:\Users\chenxuqi\Desktop\新建文件夹\testImport\sound\filters\test4cxq_1.py", 
line 15, in <module>
    from sound.effects import reverse # 这行代码被test4main.py正常调用 
ModuleNotFoundError: No module named 'sound'
'''

cmd控制台下操作的演示(运行test4main_1.py):

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

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

PS C:\Users\chenxuqi\Desktop\新建文件夹\testImport>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.367453362\pythonFiles\lib\python\debugpy\launcher' '50727' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\testImport\test4main_1.py'
初始化...我是__init__文件:sound\__init__.py
我是文件:sound\filters\__init__.py
我是文件:sound\effects\__init__.py
我是sound\effects\surround.py
我是sound\effects\reverse.py
PS C:\Users\chenxuqi\Desktop\新建文件夹\testImport> 

test4main_2.py

# from sound.filters import test4cxq
from sound.filters import test4cxq_2

sound\filters\test4cxq_2.py

# 如果一定要作为主程序来执行import语句,
# 而且同时要能被其他程序如test4main.py来调用
# 那么必须使用绝对导入,并且添加sys.path.append(os.getcwd())
# 不能使用相对导入,因为相对导入是基于当前模块的__name__,
# 而主程序的__name__总是'__main__',
# 所以不能使用相对导入,只能使用绝对导入.
# Note that relative imports are based on the 
# name of the current module. Since the name 
# of the main module is always "__main__", 
# modules intended for use as the main module 
# of a Python application must always use absolute imports.

import os
import sys
sys.path.append(os.getcwd())
print("当前路径是: ",os.getcwd())



# 添加代码sys.path.append(os.getcwd())之后,
# 以下代码就能直接作为主程序运行,同时也可以
# 被test4main.py程序调用
# 也就是说可以直接作为主程序执行,也可以被其他主程序调用
import sound.effects.surround
print(sound.effects.surround.info)
from sound.effects import reverse
print(reverse.info)



cmd控制台下操作的演示(运行test4main_2.py):

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

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

PS C:\Users\chenxuqi\Desktop\新建文件夹\testImport>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.367453362\pythonFiles\lib\python\debugpy\launcher' '50752' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\testImport\test4main_2.py'
初始化...我是__init__文件:sound\__init__.py
我是文件:sound\filters\__init__.py
当前路径是:  C:\Users\chenxuqi\Desktop\新建文件夹\testImport    
我是文件:sound\effects\__init__.py
我是sound\effects\surround.py
我是sound\effects\reverse.py
PS C:\Users\chenxuqi\Desktop\新建文件夹\testImport> 

cmd控制台下操作的演示(运行sound\filters\test4cxq_2.py):

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

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

PS C:\Users\chenxuqi\Desktop\新建文件夹\testImport>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.367453362\pythonFiles\lib\python\debugpy\launcher' '50760' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\testImport\sound\filters\test4cxq_2.py'
当前路径是:  C:\Users\chenxuqi\Desktop\新建文件夹\testImport    
初始化...我是__init__文件:sound\__init__.py
我是文件:sound\effects\__init__.py
我是sound\effects\surround.py
我是sound\effects\reverse.py
PS C:\Users\chenxuqi\Desktop\新建文件夹\testImport> 

如果在sound\filters\test4cxq_2.py中把第15行代码sys.path.append(os.getcwd())注释掉,那么test4main_2.py仍然能够正常运行,但是sound\filters\test4cxq_2.py运行会报错,如下所示:

cmd控制台下操作的演示(运行test4main_2.py):

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

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

PS C:\Users\chenxuqi\Desktop\新建文件夹\testImport>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.367453362\pythonFiles\lib\python\debugpy\launcher' '50799' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\testImport\test4main_2.py'
初始化...我是__init__文件:sound\__init__.py
我是文件:sound\filters\__init__.py
当前路径是:  C:\Users\chenxuqi\Desktop\新建文件夹\testImport    
我是文件:sound\effects\__init__.py
我是sound\effects\surround.py
我是sound\effects\reverse.py
PS C:\Users\chenxuqi\Desktop\新建文件夹\testImport> 

cmd控制台下操作的演示(运行sound\filters\test4cxq_2.py):

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

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

PS C:\Users\chenxuqi\Desktop\新建文件夹\testImport>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.367453362\pythonFiles\lib\python\debugpy\launcher' '50805' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\testImport\sound\filters\test4cxq_2.py'
当前路径是:  C:\Users\chenxuqi\Desktop\新建文件夹\testImport    
Traceback (most recent call last):
  File "c:\Users\chenxuqi\Desktop\新建文件夹\testImport\sound\filters\test4cxq_2.py", line 24, in <module>
    import sound.effects.surround
ModuleNotFoundError: No module named 'sound'
PS C:\Users\chenxuqi\Desktop\新建文件夹\testImport> 

以上说明了代码import os import sys sys.path.append(os.getcwd())的作用
参考链接: Python官方文档
代码下载链接: 源代码以及相关文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值