Python 类型错误:“模块”对象不可调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4534438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:12:05  来源:igfitidea点击:

TypeError: 'module' object is not callable

pythonsockets

提问by user551717

File "C:\Users\Administrator\Documents\Mibot\oops\blinkserv.py", line 82, in __init__
    self.serv = socket(AF_INET,SOCK_STREAM)
TypeError: 'module' object is not callable

Why am I getting this error? I'm confused.

为什么我收到这个错误?我糊涂了。

What do you need to know to answer my question?

你需要知道什么才能回答我的问题?

回答by Katriel

socketis a module, containing the class socket.

socket是一个模块,包含类socket

You need to do socket.socket(...)or from socket import socket:

你需要做socket.socket(...)from socket import socket

>>> import socket
>>> socket
<module 'socket' from 'C:\Python27\lib\socket.pyc'>
>>> socket.socket
<class 'socket._socketobject'>
>>>
>>> from socket import socket
>>> socket
<class 'socket._socketobject'>

This is what the error message means:
It says module object is not callable, because your code is calling a moduleobject. A module object is the type of thing you get when you import a module. What you were trying to do is to call a classobject within the module object that happens to have the same name as the module that contains it.

这就是错误消息的含义:
它说module object is not callable,因为您的代码正在调用模块对象。模块对象是您在导入模块时获得的事物类型。您试图做的是在模块对象中调用一个对象,该对象恰好与包含它的模块具有相同的名称。

Here is a way to logically break down this sort of error:

这是一种逻辑上分解此类错误的方法:

  • "module object is not callable. Python is telling me my code trying to call something that cannot be called. What is my code trying to call?"
  • "The code is trying to call on socket. That should be callable! Is the variable socketis what I think it is?`
  • I should print out what socket is and check print socket
  • module object is not callable。Python 告诉我我的代码试图调用无法调用的东西。我的代码试图调用什么?”
  • “代码试图调用socket。那应该是可调用的!变量socket是我认为的吗?`
  • 我应该打印出什么是套接字并检查 print socket

回答by moinudin

It seems like what you've done is imported the socketmodule as import socket. Therefore socketis the module. You either need to change that line to self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM), as well as every other use of the socketmodule, or change the import statement to from socket import socket.

您所做的似乎是将socket模块导入为import socket. 因此socket是模块。您要么需要将该行更改为self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM),以及socket模块的所有其他用途,要么将导入语句更改为from socket import socket

Or you've got an import socketafter your from socket import *:

或者你有一个import socket你的from socket import *

>>> from socket import *
>>> serv = socket(AF_INET,SOCK_STREAM)
>>> import socket
>>> serv = socket(AF_INET,SOCK_STREAM)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: 'module' object is not callable

回答by blackwind

I know this thread is a year old, but the real problem is in your working directory.

我知道这个线程已经有一年了,但真正的问题出在你的工作目录中。

I believe that the working directory is C:\Users\Administrator\Documents\Mibot\oops\. Please check for the file named socket.pyin this directory. Once you find it, rename or move it. When you import socket, socket.pyfrom the current directory is used instead of the socket.pyfrom Python's directory. Hope this helped. :)

我相信工作目录是C:\Users\Administrator\Documents\Mibot\oops\. 请检查socket.py在此目录中命名的文件。找到后,重命名或移动它。导入套接字时,socket.py将使用 from 当前目录而不是socket.pyfrom Python 的目录。希望这有帮助。:)

Note:Never use the file names from Python's directory to save your program's file name; it will conflict with your program(s).

注意:切勿使用 Python 目录中的文件名来保存程序的文件名;它会与您的程序冲突。

回答by Oscar Ardila

A simple way to solve this problem is export thePYTHONPATHvariable enviroment. For example, for Python 2.6 in Debian/GNU Linux:

解决这个问题的一个简单方法是导出PYTHONPATH变量环境。例如,对于 Debian/GNU Linux 中的 Python 2.6:

export PYTHONPATH=/usr/lib/python2.6`

In other operating systems, you would first find the location of this module or the socket.pyfile.

在其他操作系统中,您将首先找到此模块或socket.py文件的位置。

回答by zerocog

Here is another gotcha, that took me awhile to see even after reading these posts. I was setting up a script to call my python bin scripts. I was getting the module not callable too.

这是另一个问题,即使在阅读了这些帖子后,我也花了一段时间才看到。我正在设置一个脚本来调用我的 python bin 脚本。我也得到了不可调用的模块。

My zig was that I was doing the following:

我的 zig 是我正在做以下事情:

from mypackage.bin import myscript
...
myscript(...)

when my zag needed to do the following:

当我的 zag 需要执行以下操作时:

from mypackage.bin.myscript import myscript
...
myscript(...)

In summary, double check your package and module nesting.

总之,仔细检查您的包和模块嵌套。

What I am trying to do is have a scripts directory that does not have the *.py extension, and still have the 'bin' modules to be in mypackage/bin and these have my *.py extension. I am new to packaging, and trying to follow the standards as I am interpreting them. So, I have at the setup root:

我想要做的是有一个没有 *.py 扩展名的脚本目录,并且仍然有“bin”模块在 mypackage/bin 中,这些模块有我的 *.py 扩展名。我是包装新手,并在解释标准时试图遵循这些标准。所以,我在安装根目录:

setup.py
scripts/
      script1
mypackage/
   bin/
      script1.py
   subpackage1/
   subpackage_etc/

If this is not compliant with standard, please let me know.

如果这不符合标准,请告诉我。

回答by Jose Alban

Add to the main __init__.pyin YourClassParentDir, e.g.:

添加到__init__.pyYourClassParentDir 中的 main ,例如:

from .YourClass import YourClass

Then, you will have an instance of your class ready when you import it into another script:

然后,当您将它导入另一个脚本时,您将准备好您的类的一个实例:

from YourClassParentDir import YourClass

回答by Brad

Assume that the content of YourClass.pyis:

假设YourClass.py的内容是:

class YourClass:
    # ......

If you use:

如果您使用:

from YourClassParentDir import YourClass  # means YourClass.py

In this way, I got TypeError: 'module' object is not callableif you then tried to use YourClass().

这样,我得到了TypeError: 'module' object is not callableif you then try to use YourClass().

But, if you use:

但是,如果您使用:

from YourClassParentDir.YourClass import YourClass   # means Class YourClass

or use YourClass.YourClass(), it works for me.

或使用YourClass.YourClass(),它对我有用。

回答by Luke Exton

When configuring an console_scripts entrypoint in setup.py I found this issue existed when the endpoint was a module or package rather than a function within the module.

在 setup.py 中配置 console_scripts 入口点时,我发现当端点是模块或包而不是模块中的函数时存在此问题。

Traceback (most recent call last):
   File "/Users/ubuntu/.virtualenvs/virtualenv/bin/mycli", line 11, in <module>
load_entry_point('my-package', 'console_scripts', 'mycli')()
TypeError: 'module' object is not callable

For example

例如

from setuptools import setup
setup (
# ...
    entry_points = {
        'console_scripts': [mycli=package.module.submodule]
    },
# ...
)

Should have been

本来应该

from setuptools import setup
setup (
# ...
    entry_points = {
        'console_scripts': [mycli=package.module.submodule:main]
    },
# ...
)

So that it would refer to a callable function rather than the module itself. It seems to make no difference if the module has a if __name__ == '__main__':block. This will not make the module callable.

这样它就会引用一个可调用的函数而不是模块本身。如果模块有一个if __name__ == '__main__':块似乎没有区别。这不会使模块可调用。

回答by umairhhhs

I guess you have overridden the builtin function/variable or something else "module" by setting the global variable "module". just print the module see whats in it.

我猜您已经通过设置全局变量“模块”覆盖了内置函数/变量或其他“模块”。只需打印模块,看看里面有什么。

回答by Ayesha Siddiqa

check the import statements since a module is not callable. In Python, everything (including functions, methods, modules, classes etc.) is an object.

检查导入语句,因为模块不可调用。在 Python 中,一切(包括函数、方法、模块、类等)都是一个对象。