2、引入父级模块或者包
-----------------------------------------引入父级目录
目录结构:
print_father.py引入上级目录father.py模块的sentence变量,代码如下:
#注:需要引入的文件 E:\test\test176\test186\grandfather.py
import sys
sys.path.append(r'E:\test\test176\test186') #python搜索环境变量中增加需要引入模块所在路径
from grandfather import sentence
print(sentence)
-----------------------
print_grandfather.py引入上级目录grandfather.py模块的sentence变量,代码如下:
import sys
sys.path.append(r'E:\test\test176')
from grandfather import sentence
print(sentence)
-----------------------------------------------------------------------引入父级子目录
引入父级子目录,并且把增加变量写在包所属模块__init__.py内
目录结构: