问题:TypeError:“模块”对象不可调用
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
为什么会出现此错误?我糊涂了。
您需要知道什么才能回答我的问题?
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?
回答 0
socket
是一个包含class的模块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'>
这就是错误消息的含义:
它表示为module object is not callable
,因为您的代码正在调用模块对象。模块对象是导入模块时得到的东西的类型。您试图做的是在模块对象中调用恰好与包含它的模块同名的类对象。
这是一种从逻辑上分解这种错误的方法:
- “
module object is not callable
。Python告诉我我的代码试图调用无法调用的内容。我的代码试图调用什么?”
- “代码正在尝试调用
socket
。那应该是可以调用的!变量socket
是我认为的吗?”
- 我应该打印出什么是插座并检查
print socket
socket
is a module, containing the class socket
.
You need to do socket.socket(...)
or 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 module object. 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 class object within the module object that happens to have the same name as the module that contains it.
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 socket
is what I think it is?`
- I should print out what socket is and check
print socket
回答 1
假设YourClass.py的内容是:
class YourClass:
# ......
如果您使用:
from YourClassParentDir import YourClass # means YourClass.py
这样,我得到TypeError:如果您随后尝试使用‘module’对象,则该对象不可调用YourClass()
。
但是,如果您使用:
from YourClassParentDir.YourClass import YourClass # means Class YourClass
或使用YourClass.YourClass()
,它对我有用。
Assume that the content of YourClass.py is:
class YourClass:
# ......
If you use:
from YourClassParentDir import YourClass # means YourClass.py
In this way, I got TypeError: ‘module’ object is not callable if you then tried to use YourClass()
.
But, if you use:
from YourClassParentDir.YourClass import YourClass # means Class YourClass
or use YourClass.YourClass()
, it works for me.
回答 2
添加到__init__.py
YourClassParentDir中的主目录,例如:
from .YourClass import YourClass
然后,将类导入另一个脚本时,您将准备好该类的实例:
from YourClassParentDir import YourClass
Add to the main __init__.py
in YourClassParentDir, e.g.:
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
回答 3
这是另一个陷阱,即使阅读了这些帖子,我也花了一段时间。我正在设置一个脚本来调用我的python bin脚本。我也无法调用该模块。
我的字形是我正在执行以下操作:
from mypackage.bin import myscript
...
myscript(...)
当我的Zag需要执行以下操作时:
from mypackage.bin.myscript import myscript
...
myscript(...)
总之,请仔细检查您的程序包和模块嵌套。
我正在尝试做的是拥有一个没有* .py扩展名的脚本目录,并且仍然将“ bin”模块放入mypackage / bin中,而这些扩展名具有我的* .py扩展名。我是包装的新手,在解释标准时要遵循这些标准。所以,我在安装根目录下:
setup.py
scripts/
script1
mypackage/
bin/
script1.py
subpackage1/
subpackage_etc/
如果这不符合标准,请告诉我。
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.
My zig was that I was doing the following:
from mypackage.bin import myscript
...
myscript(...)
when my zag needed to do the following:
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:
setup.py
scripts/
script1
mypackage/
bin/
script1.py
subpackage1/
subpackage_etc/
If this is not compliant with standard, please let me know.
回答 4
看来您完成的操作是将socket
模块导入为import socket
。因此socket
是模块。您或者需要将该行更改为self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
,以及将该socket
模块的所有其他用法更改为1,或者将import语句更改为from socket import socket
。
或者你有一个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
It seems like what you’ve done is imported the socket
module as import socket
. Therefore socket
is 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 socket
module, or change the import statement to from socket import socket
.
Or you’ve got an import socket
after your 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
回答 5
我知道这个线程已经使用了一年,但是真正的问题出在您的工作目录中。
我相信工作目录是C:\Users\Administrator\Documents\Mibot\oops\
。请检查socket.py
在此目录中命名的文件。找到它后,对其进行重命名或移动。导入套接字时,socket.py
将使用当前目录中的内容,而不是使用socket.py
Python目录中的内容。希望这会有所帮助。:)
注意:切勿使用Python目录中的文件名来保存程序的文件名。它将与您的程序冲突。
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.py
in this directory. Once you find it, rename or move it. When you import socket, socket.py
from the current directory is used instead of the socket.py
from Python’s directory. Hope this helped. :)
Note: Never use the file names from Python’s directory to save your program’s file name; it will conflict with your program(s).
回答 6
在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
例如
from setuptools import setup
setup (
# ...
entry_points = {
'console_scripts': [mycli=package.module.submodule]
},
# ...
)
本来应该
from setuptools import setup
setup (
# ...
entry_points = {
'console_scripts': [mycli=package.module.submodule:main]
},
# ...
)
这样它将引用可调用函数,而不是模块本身。如果模块具有if __name__ == '__main__':
块,这似乎没有什么区别。这将不会使该模块可调用。
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.
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.
回答 7
我想您已经通过设置全局变量“ module”来覆盖了内置函数/变量或其他“ module”。只需打印模块,看看其中有什么。
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.
回答 8
由于模块不可调用,请检查导入语句。在Python中,所有东西(包括函数,方法,模块,类等)都是一个对象。
check the import statements since a module is not callable.
In Python, everything (including functions, methods, modules, classes etc.) is an object.
回答 9
解决此问题的一种简单方法是导出PYTHONPATH
变量环境。例如,对于Debian / GNU Linux中的Python 2.6:
export PYTHONPATH=/usr/lib/python2.6`
在其他操作系统中,您首先要找到该模块或socket.py
文件的位置。
A simple way to solve this problem is export thePYTHONPATH
variable enviroment. For example, for Python 2.6 in Debian/GNU Linux:
export PYTHONPATH=/usr/lib/python2.6`
In other operating systems, you would first find the location of this module or the socket.py
file.