文件之間的引用分爲:
(1)同一文件目錄下:
import helloWorld
from core import func
(2)不同文件目錄下:
需要先獲取當前文件所在的文件夾路徑,然後添加到系統路徑sys.path,這樣就可以訪問了。
import sys
import os
dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,dir)
print(dir)
print(sys.path)
from core import main
main.run()
生成程序的目錄結構可以參考:https://segmentfault.com/a/1190000009163772