python避免重复导入模块_Python 模块定义、导入、优化详解

一、定义

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

包:用来从逻辑上组织模块的,本质就是一个目录(必须带有一个 __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 defsay_hello():3 print('hello alex')4

5 deflogger():6 pass

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

1 name = 'alex'

2 defsay_hello():3 print('hello alex')4

5 deflogger():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 importmodule3

4 #调用 module 模块中的变量和函数

5 print(module.name) #alex

6 module.say_hello() #hello alex

7

8 #定义 logger() 函数

9 deflogger():10 print("in the main_module")11

12 #logger() # 调用 logger 函数

13 #此时返回”in the main_module“,即该文件中的logger函数覆盖了前面调用 module 模块中的同名函数

14

15 #如何解决这个冲突呢?我们可以使用另一种导入方法,即给 logger 函数取别名:

16 from module importlogger as logger117

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 defsay_hello():3 print('hello alex')4

5 deflogger():6 print('in the module1')

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

1 importsys,os2 #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 importmodule113 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 deftest():2 print('in the __init__')

mo_test.py 文件内容如下:

1 import __init__

2

3 deflogger():4 __init__.test() #导入__init__模块下的 test() 函数

5 print('in the logger')6

7 defa():8 __init__.test() #导入__init__模块下的 test() 函数

9 print("in the a")10

11 logger()12 a()

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

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

1 #导入模块中指定的函数

2 from __init__ importtest3

4 deflogger():5 test() #直接调用 test() 函数

6 print('in the logger')7

8 defa():9 test() #直接调用 test() 函数

10 print("in the a")11

12 logger()13 a()

- End -

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytest是一个Python测试框架,它是基于`unittest`和`nose`这两个Python测试框架的经验基础上发展而来的。相比于其他测试框架,pytest具有更简洁、更灵活的语法,让编写和管理测试用例更加方便。 在使用pytest之前,我们需要将其导入到我们的Python项目。可以通过`pip`命令来安装pytest包,具体命令为`pip install pytest`。安装完成后,我们可以在Python代码通过`import pytest`语句来引入pytest模块导入pytest后,我们就可以使用pytest提供的丰富的装饰器和断言函数来编写测试用例。pytest支持使用`@pytest.fixture`装饰器定义测试用例前后需要执行的操作,比如创建数据库连接等。使用`@pytest.mark.parametrize`装饰器可以方便地对测试用例进行参数化,减少重复性的代码。 pytest还提供了丰富的断言函数,用于验证测试结果是否符合预期。常用的断言函数有`assert`、`assertEqual`、`assertTrue`等。使用这些断言函数可以方便地检查测试用例的执行结果,并在测试失败时输出详细的错误信息,帮助我们快速定位问题。 除了基本的测试用例编写和断言功能外,pytest还提供了插件机制,可以通过插件扩展pytest的功能。例如,`pytest-html`插件可以生成HTML格式的测试报告,`pytest-xdist`插件可以实现用例的并行执行,提高测试效率。 总结来说,pytest是一个功能强大、灵活且易于使用的Python测试框架。通过导入pytest模块,并利用它提供的装饰器和断言函数,我们可以高效地编写和管理测试用例。同时,借助插件机制,我们可以扩展pytest的功能,满足更多的测试需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值