python不同目录间模块调用

Posted 苦海舟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于python不同目录间模块调用的主要内容,如果未能解决你的问题,请参考以下文章

python 不同目录间的模块调用

Python之路-目录规范和不同目录间进行模块调用

软件目录结构规范以及在不同目录间进行模块调用

python之模块和包

python3 不同目录下的模块调用

Python 模块