Python 模块定义、导入、优化详解

 

 

一、定义

 

  1. 模块:用来从逻辑上组织 python 代码(变量,函数,类, 逻辑:实现一个功能),本质就是 .py 结尾的 python文件(例如:test.py文件,对应的模块名:test) 
  2. :用来从逻辑上组织模块的,本质就是一个目录(必须带有一个 __init__.py 文件)

 

二、导入方法

 

# 导入单个模块

import module_name

 

# 一次导入多个模块

import module1_name,module2_name

 

# 从module_a模块导入其所有代码

from module_a import *

 

# 从module_a模块导入多个变量/函数/类

from module_a import m1,m2,m3

 

# 给logger模块取别名为logger1并导入

from module_a import logger as logger1

 

# 从当前目录下导入test1模块

from . import test1  

 

 

三、Import本质(路径搜索和搜索路径)

 

1. 导入模块的本质就是把python文件解释一遍 

 

  • 导入整个模块 = 导入整个模块中全部的代码

 import test   test = “test.py all code”

 

  • 导入模块中的某个变量或函数 = 只导入该变量或函数,可直接调用

   from test import m1    m1 = “code”

 

  • 导入模块(需要先找到)----> 模块对应的python文件 ----> 该python文件的路径 ----> 可用 sys.path 方法获取路径

   import module ----> module_name.py ----> module_name.py 的路径 ----> sys.path

 

2. 导入包的本质就是执行该包下的__init__.py文件

 

 

四、同级导入模块

 

假设,有一个名为 module_test 的目录,该目录下包含一个 module.py 文件和一个 main_module.py 文件。

module.py文件中内容数据如下:

1 name = 'alex'
2 def say_hello():
3     print('hello alex')
4 
5 def logger():
6     pass

 

main_module.py 文件中需要调用 module 模块,代码实现如下:

1 name = 'alex'
2 def say_hello():
3     print('hello alex')
4 
5 def logger():
6     print('in the module')

 

假如 module 模块中有好几个函数,你想要导入所有的函数,可使用:from module import * 

如果当前脚本文件中已经定义了logger()函数,这时要调用的 module.py 文件中又已经包含了一个同名的 logger() 函数,则调用 logger() 函数时会产生冲突,故为了避免冲突,此方法要慎用。

 

如何避免这种冲突呢?我们可以使用另一种导入方法,即给 logger 函数取别名:from module import logger as logger1    

main_module.py 文件代码如下:

 1 # 导入module模块(文件名:module.py,对应的模块名:module)
 2 import module
 3 
 4 # 调用 module 模块中的变量和函数
 5 print(module.name)     # alex
 6 module.say_hello()     # hello alex
 7 
 8 # 定义 logger() 函数
 9 def logger():
10     print("in the main_module")
11 
12 # logger()     # 调用 logger 函数
13 # 此时返回”in the main_module“,即该文件中的logger函数覆盖了前面调用 module 模块中的同名函数
14 
15 # 如何解决这个冲突呢?我们可以使用另一种导入方法,即给 logger 函数取别名:
16 from module import logger as logger1    
17 
18 # 此时再调用改了别名的logger函数logger1
19 logger1()     
20 # 打印 module 模块中的 logger()函数的返回结果 ”in the module"

 

 

五、不同级导入模块

 

案例:路径为 day5\module_test\main_module1.py 的 main_module1.py文件导入其父级目录 day5目录下的 module1 模块(即day5\module1.py)

module1.py 文件内容数据如下:

1 name = 'alex'
2 def say_hello():
3     print('hello alex')
4 
5 def logger():
6     print('in the module1')

 

main_module1.py 文件中需要调用 module1 模块,代码实现如下:

 1 import sys,os
 2 # print(sys.path)                        # 打印当前相对路径
 3 # C:\Users\Administrator\PycharmProjects\test\day5\module_test\main_module1.py
 4 
 5 # dirname作用是返回上级目录名,两层dirname代表返回上上级目录,即返回到了day5目录; os.path.abspath(__file__)是打印当前绝对路径 
 6 
 7 x = os.path.dirname( os.path.dirname( os.path.abspath(__file__) ) )
 8 # C:\Users\Administrator\PycharmProjects\test\day5
 9 
10 # 添加路径,再导入module1 模块(day5\module1.py)
11 sys.path.append(x)
12 import module1
13 print(module1.name)     # alex
14 module1.say_hello()     # hello alex
15 module1.logger()        # in the module1

 

 

六、导入优化

 

假设,有一个名为 module_optimize 的包,该包下包含一个__init__.py 文件和一个主逻辑文件 mo_test.py 。

__init__.py 文件内容如下:

1 def test():
2     print('in the __init__')

 

mo_test.py 文件内容如下:

 1 import __init__
 2 
 3 def logger():
 4     __init__.test()        # 导入__init__模块下的 test() 函数
 5     print('in the logger')
 6 
 7 def a():
 8     __init__.test()        # 导入__init__模块下的 test() 函数
 9     print("in the a")
10 
11 logger()
12 a()

可见,logger() 函数和 a() 函数都需要调用到 __init__ 模块下的 test() 函数。这时候,当程序执行的时候,就要重复从 __init__.py 文件中检索 test() 函数,造成程序运行时间的浪费。那么,我们可以如何优化呢?

 

为了避免重复检索模块而浪费运行时间,我们可以使用 from __init__ import test 方法。具体代码如下:

 1 # 导入模块中指定的函数
 2 from __init__ import test
 3 
 4 def logger():
 5     test()               # 直接调用 test() 函数
 6     print('in the logger')
 7 
 8 def a():
 9     test()               # 直接调用 test() 函数
10     print("in the a")
11 
12 logger()
13 a()

 

 

- End -

 

posted @ 2019-04-28 16:28  宁晃  阅读(446)  评论(0编辑  收藏  举报