2、引入父级模块或者包

-----------------------------------------引入父级目录
目录结构:


print_father.py引入上级目录father.py模块的sentence变量,代码如下:
#注:需要引入的文件 E: est est176 est186grandfather.py

import sys
sys.path.append(r'E: est est176 est186')               #python搜索环境变量中增加需要引入模块所在路径
from grandfather import sentence
print(sentence)


-----------------------
print_grandfather.py引入上级目录grandfather.py模块的sentence变量,代码如下:

import sys
sys.path.append(r'E: est est176')
from grandfather import sentence
print(sentence)

-----------------------------------------------------------------------引入父级子目录

引入父级子目录,并且把增加变量写在包所属模块__init__.py内


目录结构:

 

原文地址:https://www.cnblogs.com/whitemouseV2-0/p/9925374.html