当在下级中引用上级时,使用相对导包会出错,SystemError: Parent module '' not loaded, cannot perform relative import

python中,下级模块引用上级模块:SystemError: Parent module '' not loaded, cannot perform relative import-LMLPHP

python中,下级模块引用上级模块:SystemError: Parent module '' not loaded, cannot perform relative import-LMLPHP

运行test_count.py文件

python中,下级模块引用上级模块:SystemError: Parent module '' not loaded, cannot perform relative import-LMLPHP

解决方案:在test_count.py文件中加入如下代码,将上上级路径加入到环境变量中

import os
import sys
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, path)

path的值打印出来是F:\uncleyong,此时直接import count就不会出问题了

python中,下级模块引用上级模块:SystemError: Parent module '' not loaded, cannot perform relative import-LMLPHP

04-25 01:29