python 一個包中的文件調用另外一個包文件 實例
阿新 • • 發佈:2017-05-17
pat def pre img end import test imp clas
python不同文件夾中模塊的引用調用順序,被調用的模塊中①有類的 模塊.類().方法() ②無類的:模塊.方法()
test包中testIm.py 調用 test1包中testIm1.py中的方法craw1()方法,輸出結果:
testIm.py
# coding:utf-8
# testIm.py位於test包中,要調用test1包中的testIm1.py中的方法craw1()方法
import sys sys.path.append(‘C:\\pythonwork\\test1‘) #加入路徑,添加目錄
# print sys.path import testIm1 classTest_Im(object): def craw(self): print ‘This is testIm‘ testIm1.Test_Im1().craw1() if __name__ == ‘__main__‘: im = Test_Im() im.craw()
testIm1.py
#coding:utf-8
# testIm1.py位於test1包中
class Test_Im1(object): def craw1(self): print ‘This is testIm1‘ if __name__== ‘__main__‘: im = Test_Im1() im.craw1()
參考: python 不同文件夾中模塊的引用
python 一個包中的文件調用另外一個包文件 實例