Python中包(package)的调用方式,包package,          


                     Python中包(package)的调用方式

                                          作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.什么是Python Package

  如何区分你看到的目录是一个Python Package包呢?其实很简单,你只要看这个名录下是否有“__init__.py”这个文件就好了,如果有那么就是Python Package包,如果没有,就说嘛你看到的就是个普通的目录,如下图,你就可以看出来"calcuate"和"Log"就是一个Python Package包,而"yinzhengjie"就是一个目录,而判断的依据就是是否包含_init__.py文件。"yinzhengjie"这个目录下包含三个文件,即“calcuate”,“Log”和“bin.py"文件。

技术分享图片

二.主程序调用包中的模块

  目录结构如上图所示,以下是其中各个文件中的代码。

技术分享图片
1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 #@author :yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/5 #EMAIL:[email protected]6 7 8 def Add(x,y):9     return x + y
sc_cal技术分享图片
 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 import logging as log 8  9 def GetLogger():10     logger = log.getLogger()11 12     fh = log.FileHandler("log.txt")13 14     logger.addHandler(fh)15 16     return logger
logger

  以下是bin主程序的代码。

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 from Log import logger 8  9 from  calcuate import sc_cal10 11 obj = logger.GetLogger()12 13 obj.error("This yinzhengjie‘s test error !")14 15 s1 = sc_cal.Add(100,200)16 17 print(s1)18 19 20 21 #以上代码执行结果如下:22  300

  执行主程序代码之后,会在执行的目录中生成一个名称为“log.txt”的文件。

技术分享图片
1 This yinzhengjie‘s test error !
log.txt

三.包中模块的调用

技术分享图片

  目录结构如上图所示,以下是其中各个文件中的代码。

技术分享图片
1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 #@author :yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/5 #EMAIL:[email protected]6 7 8 def Add(x,y):9     return x + y
sc_cal技术分享图片
1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 #@author :yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/5 #EMAIL:[email protected]6 7 def Product(x,y):8     return x * y
dome技术分享图片
 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 import logging as log 8  9 from Log import dome10 11 def GetLogger():12     logger = log.getLogger()13 14     fh = log.FileHandler("log.txt")15 16     logger.addHandler(fh)17 18     return logger19 20 print(dome.Product(5,6))
logger

  以下是bin主程序的代码。

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 from Log import logger 8  9 from  calcuate import sc_cal10 11 obj = logger.GetLogger()12 13 obj.error("This yinzhengjie‘s test error !")14 15 s1 = sc_cal.Add(100,200)16 17 print(s1)18 19 20 21 22 #以上代码执行结果如下:23 3024 300
技术分享图片
1 This yinzhengjie‘s test error !2 This yinzhengjie‘s test error !
log.txt

四.同级目录下的被调用模块之间的相互调用

技术分享图片

  目录结构如上图所示,以下是其中各个文件中的代码。

1>.Log 包中的源代码

技术分享图片
1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 #@author :yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/5 #EMAIL:[email protected]6 7 def Product(x,y):8     return x * y
dome技术分享图片
 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 import logging as log 8  9 from Log import dome10 11 def GetLogger():12     logger = log.getLogger()13 14     fh = log.FileHandler("log.txt")15 16     logger.addHandler(fh)17 18     return logger19 20 print(dome.Product(5,6))
logger

2>.calcuate 包中的源代码

技术分享图片
1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 #@author :yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/5 #EMAIL:[email protected]6 7 8 def Add(x,y):9     return x + y
sc_cal

3>.bin 包中的源代码

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 import os,sys 8  9 10 AbsolutePath = os.path.abspath(__file__)           #将相对路径转换成绝对路径11 12 SuperiorCatalogue = os.path.dirname(AbsolutePath)   #相对路径的上级路径13 14 BaseDir = os.path.dirname(SuperiorCatalogue)        #在“SuperiorCatalogue”的基础上在脱掉一层路径,得到我们想要的路径。15 16 sys.path.insert(0,BaseDir)                          #将我们取出来的路径加入到Python的命名空间去,并将该目录插入在第一个位置中。17 18 from Log import logger19 20 from  calcuate import sc_cal21 22 23 obj = logger.GetLogger()24 25 obj.error("This yinzhengjie‘s test error !")26 27 s1 = sc_cal.Add(100,200)28 29 print(s1)30 31 32 33 #以上代码执行结果如下:34 3035 300

  执行主程序代码之后,会在执行的目录中生成一个名称为“log.txt”的文件。

技术分享图片
1 This yinzhengjie‘s test error !
log.txt

五.包的多级调用

技术分享图片

  目录结构如上图所示,以下是其中各个文件中的代码。

技术分享图片
 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 import logging as log 8  9 10 def GetLogger():11     logger = log.getLogger()12 13     fh = log.FileHandler("log.txt")14 15     logger.addHandler(fh)16 17     return logger
logger

  主程序代码如下:

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7  8 from Log.Loger import logger as log 9 10 obj = log.GetLogger()11 12 obj.error("This yinzhengjie‘s test error !")

  执行主程序代码之后,会在执行的目录中生成一个名称为“log.txt”的文件。

技术分享图片
1 This yinzhengjie‘s test error !
log.txt

六.__name__的应用

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 print("This is Test Page !") 8  9 def foo():10     print("ok")11 12 13 14 if __name__=="__main__":  # 推荐使用这种方式调试代码,只有执行当前模块的人才会执行以下代码,如果是别人调用该模块,以下的代码是不会被执行的!15     foo()16 17 print(__name__)18 19 20 21 22 23 24 #以上代码执行结果如下:25 This is Test Page !26 ok27 __main__

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7  8 import name 9 10 11 12 #以上代码执行结果如下:13 This is Test Page !14 name

Python中包(package)的调用方式

评论关闭