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官方文档