2、引入父級模組或者包
阿新 • • 發佈:2018-11-07
-----------------------------------------引入父級目錄
目錄結構:
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內
目錄結構: