TypeError: 'module' object is not callable

前端 未结 11 1715
耶瑟儿~
耶瑟儿~ 2020-11-22 12:06
File \"C:\\Users\\Administrator\\Documents\\Mibot\\oops\\blinkserv.py\", line 82, in __init__
    self.serv = socket(AF_INET,SOCK_STREAM)
TypeError: \'module\' objec         


        
相关标签:
11条回答
  • 2020-11-22 12:11

    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
    
    0 讨论(0)
  • 2020-11-22 12:12

    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
    0 讨论(0)
  • 2020-11-22 12:17

    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.

    0 讨论(0)
  • 2020-11-22 12:17

    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.

    0 讨论(0)
  • 2020-11-22 12:20

    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.

    0 讨论(0)
  • 2020-11-22 12:22

    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.

    0 讨论(0)
提交回复
热议问题