Python“TypeError:”module“object is not callable”错误及解决方案

Python函数通常由模块提供。模块提供了对不同函数、变量等进行分类的能力。当我们处理模块和函数时,可能会出现如下错误 TypeError: 'module' object is not callable .

null

什么是“TypeError:’module’对象不可调用”

在使用函数时,我们通常使用模块来导入和使用它们。这可能会造成混乱。在某些情况下,模块名和函数名可能相同。例如 getopt 模块提供 getopt() 可能造成混淆的功能。

我们可以在下面的截图中看到 TypeError:'module' object is not callable . callable意味着给定的python对象可以像函数一样调用,但是在这个错误中,我们警告说给定的模块不能像函数一样调用。

What Is "TypeError: 'module' object is not callable"
什么是“TypeError:’module’对象不可调用”

通过导入函数求解

我们可以解决这个错误 通过重要的功能直接在乞讨的源代码下面的导入语句部分。我们将使用 from import 声明。from用于指定模块名和 import is used to point function name. In this example, we will import the function named chdir() from the 操作系统模块如下。

from os import chdir

...

...

...

chdir("/bin/")

或者,我们可以为名为 chdir() . 我们将使用 as 语句指定新名称 ch 在这个例子中。

from os import chdir as ch

...

...

...

ch("/bin")

用模块名调用解决

另一个解决方案 TypeError: 'module' object is not callable 导入所需库后直接调用函数时出错。在本例中,我们将使用 import 语句,然后将模块名与要调用的函数一起使用。在本例中,我们将导入模块 os 和运行函数 chdir() 从操作系统模块。

import os# ...# ...# ...os.chdir("/bin/")

相关文章: 如何在Windows中安装Pip?

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享