迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

解决 Python 中 TypeError: Module Object Is Not Callable 错误

作者:迹忆客 最近更新:2023/07/05 浏览次数:

每种编程语言都会遇到很多错误。 有些发生在编译时,有些发生在运行时。

本文将讨论 TypeError: 'module' object is not callable。 当类/方法和模块具有相同的名称时,就会出现此错误; 由于名称相同,我们会混淆它们。

当我们导入模块而不是方法时,调用该模块将给出错误,因为该模块不可调用。 我们只能调用方法。


Python 中 TypeError: 'module' object is not callable 的原因

我们将在下面的代码中导入一个内置模块,socket。 该模块还包含一个名为 socket() 的类。

这里,方法名和类名是相同的。 如果我们只导入套接字并调用它,解释器将抛出 TypeError: 'module' object is not callable

示例代码:

#Python 3.x
import socket
socket()

输出:

#Python 3.x
Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: 'module' object is not callable

如果是我们自己定义的自定义模块,我们也可能会遇到此错误。 我们创建了以下代码并将其保存在名为 infomodule.py 的文件中。

示例代码:

#Python 3.x
def infomodule():
    info='meeting at 10:00 am.'
    print(info)

然后我们创建了另一个Python文件并编写了以下代码,调用模块infomodule.py。 我们将得到 TypeError: 'module' object is not callable

示例代码:

#Python 3.x
import infomodule
Print(infomodule())

输出:

#Python 3.x
Traceback (most recent call last):
File "mycode.py", line 3, in <module>
 print(infomodule())
TypeError: 'module' object is not callable

解决 Python 中 TypeError: 'module' object is not callable

从模块调用方法/类

要修复此错误,我们可以从模块导入类,而不是直接导入模块。 它将修复 TypeError :'module' object is not callable

到这里,我们就成功创建了一个socket类的对象。

示例代码:

#Python 3.x
import socket
socket.socket()

输出:

<socket.socket fd=876, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>

对于自定义模块,我们可以类似地修复错误。 这里,我们调用了模块 infomodule 的方法 infomodule()

示例代码:

import infomodule
print(infomodule.infomodule())

输出:

meeting at 10:00 am.

从模块导入方法/类

修复此错误的另一种方法是从模块导入类并创建其对象。

在这里,我们从套接字模块导入了套接字类并创建了它的对象。

示例代码:

#Python 3.x
from socket import socket
socket()

输出:

<socket.socket fd=876, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>

对于自定义模块,我们也将遵循相同的过程。 在这里,我们从模块导入方法并调用它。

示例代码:

#Python 3.x
from infomodule import infomodule
print(infomodule())

输出:

meeting at 10:00 am.

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Python 中错误 Address Already in Use

发布时间:2023/07/09 浏览次数:173 分类:Python

我们将通过示例介绍Python中何时出现 Address already in use 错误以及如何解决。Python 中的错误Address already in use 本文将讲述运行使用端口的程序时发生的Python堆栈错误。

Python 中错误 ValueError: Math Domain Error

发布时间:2023/07/09 浏览次数:607 分类:Python

在本篇文章中,我们的目标是探索解决 Python 中的 ValueError: math domain error 错误的不同方法。当编码方面数学(基础或高级)的使用存在固有缺陷时,Python 中通常会引发 ValueError: math domain error 错

Python 错误 Name xrange Is Not Defined

发布时间:2023/07/09 浏览次数:153 分类:Python

本篇文章将介绍如何解决 Python 中 name 'xrange' is not defined 的错误。解决Python中name 'xrange' is not defined错误 让我们尝试理解为什么会发生这个特定的错误。 让我们首先尝试复制这个问题。

Python 中错误 AttributeError: __Enter__

发布时间:2023/07/09 浏览次数:2241 分类:Python

在 Python 中,AttributeError 是在未定义 __enter__ 函数的情况下通过 with 语句使用类的对象时导致的错误。

Python 错误 Error: Invalid Command Bdist_wheel

发布时间:2023/07/09 浏览次数:847 分类:Python

在 Python 中构建 wheel 时,有时 setup.py 可能会退出并出现错误 invalid command 'bdist_wheel'。 本篇文章将讨论在 Python 中解决此问题的可能解决方案。安装wheel包来修复Python中 Error:invalid command 'bdist_

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便