python不同目錄間模塊調用


前置:

sys.path是python的搜索模塊的路徑集。

 

以下是目錄結構:

 

1、首先同一目錄下的模塊間調用:b目錄下Math_3.py調用Math_4.py

 1 import sys
 2 print('the path of Math_3 is {}'.format(sys.path))
 3 from Math_4 import Math_4
 4 class Math_3:
 5 
 6     def __init__(self, a, b):
 7         self.a = a
 8         self.b = b
 9         
10     def mul(self):
11         return self.a * self.b
12     
13     m4 = Math_4(6, 2)
14     print('the value of m4 is {}'.format(m4.div()))
15         
16 if __name__ == '__main__':
17     m3 = Math_3(3, 2)
18     print('the value of m3 is {}'.format(m3.mul()))
19 
20 #運行結果
21 the path of Math_3 is ['D:\\PyEx\\math\\b', 'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib', 'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
22 the value of m4 is 3.0
23 the value of m3 is 6

 

2、基於第1步同級別目錄下的模塊調用:b目錄下Math_3.py調用a目錄下的Math_2.py

 1 import sys
 2 #表示導入當前文件的上層目錄到搜索路徑中
 3 sys.path.append('..')
 4 print('the path of Math_3 is {}'.format(sys.path))
 5 from a.Math_2 import Math_2
 6 from Math_4 import Math_4
 7 class Math_3:
 8 
 9     def __init__(self, a, b):
10         self.a = a
11         self.b = b
12         
13     def mul(self):
14         return self.a * self.b
15         
16     m2 = Math_2(3, 1)
17     print('the value of m2 is {}'.format(m2.sub()))
18     
19     m4 = Math_4(6, 2)
20     print('the value of m4 is {}'.format(m4.div()))
21         
22 if __name__ == '__main__':
23     m3 = Math_3(3, 2)
24     print('the value of m3 is {}'.format(m3.mul()))
25 
26 #運行結果
27 the path of Math_3 is ['D:\\PyEx\\math\\b', 'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib', 'C:\\Python36', 'C:\\Python36\\lib\\site-packages', '..']
28 the value of m2 is 2
29 the value of m4 is 3.0
30 the value of m3 is 6

 

3、基於第2步,最外層目錄math下的test_math.py調用b目錄下的Math_3.py

 1 import sys
 2 print('the path of test_math is {}'.format(sys.path))
 3 from b.Math_3 import Math_3
 4 m1 = Math_1(1, 2)
 5 m3 = Math_3(4, 5)
 6 print('the value of m3 is {}'.format(m3.mul()))
 7 
 8 #運行結果:
 9 the path of test_math is ['D:\\PyEx\\math', 'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib', 'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
10 the path of Math_3 is ['D:\\PyEx\\math', 'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib', 'C:\\Python36', 'C:\\Python36\\lib\\site-packages', '..']
11 Traceback (most recent call last):
12   File "test_math.py", line 4, in <module>
13     from b.Math_3 import Math_3
14   File "D:\PyEx\math\b\Math_3.py", line 5, in <module>
15     from Math_4 import Math_4
16 ModuleNotFoundError: No module named 'Math_4'

運行結果報錯,根據14、15行信息,原因是在Math_3.py中導入的同一目錄下的Math_4.py出錯了,通過分析運行結果中打印出來的sys.path,是由於程序搜索模塊的路徑集中不包含D:\\PyEx\\math\\b,所以修改Math_3.py的代碼如下:

 1 import sys
 2 sys.path.append('..')
 3 print('the path of Math_3 is {}'.format(sys.path))
 4 from a.Math_2 import Math_2
 5 #修改后
 6 from b.Math_4 import Math_4
 7 class Math_3:
 8 
 9     def __init__(self, a, b):
10         self.a = a
11         self.b = b
12         
13     def mul(self):
14         return self.a * self.b
15         
16     m2 = Math_2(3, 1)
17     print('the value of m2 is {}'.format(m2.sub()))
18     
19     m4 = Math_4(6, 2)
20     print('the value of m4 is {}'.format(m4.div()))
21         
22 if __name__ == '__main__':
23     m3 = Math_3(3, 2)
24     print('the value of m3 is {}'.format(m3.mul()))

 

4、基於以上修改再次運行test_math.py文件,其結果如下

the path of test_math is ['D:\\PyEx\\math', 'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib', 'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
the path of Math_3 is ['D:\\PyEx\\math', 'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib', 'C:\\Python36', 'C:\\Python36\\lib\\site-packages', '..']
the value of m2 is 2
the value of m4 is 3.0
the value of m1 is 3
the value of m3 is 20


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM