TypeError: 'module' object is not callable

PythonSockets

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

Assume that the content of YourClass.py is:

class YourClass:
    # ......

If you use:

from YourClassParentDir import YourClass  # means YourClass.py

In this way, you will get TypeError: 'module' object is not callable if you then tried to call YourClass().

But, if you use:

from YourClassParentDir.YourClass import YourClass   # means Class YourClass

or use YourClass.YourClass(), it works.

Solution 3 - Python

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

Solution 4 - Python

Short answer: You are calling a file/directory as a function instead of real function

Read on:

This kind of error happens when you import module thinking it as function and call it. So in python module is a .py file. Packages(directories) can also be considered as modules. Let's say I have a create.py file. In that file I have a function like this:

#inside create.py
def create():
  pass

Now, in another code file if I do like this:

#inside main.py file
import create
create() #here create refers to create.py , so create.create() would work here

It gives this error as am calling the create.py file as a function. so I gotta do this:

from create import create
create() #now it works.

Solution 5 - Python

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.

Solution 6 - Python

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

Solution 7 - 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).

Solution 8 - Python

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.

Solution 9 - Python

I faced the same problem. then I tried not using from YourClass import YourClass

I just copied the whole code of YourClass.py and run it on the main code (or current code).it solved the error

Solution 10 - Python

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.

Solution 11 - Python

Here's a possible extra edge case that I stumbled upon and was puzzled by for a while, hope it helps someone:

In some_module/a.py:

def a():
   pass

In some_module/b.py:

from . import a

def b():
   a()

In some_module/__init__.py:

from .b import b
from .a import a

main.py:

from some_module import b

b()

Then because when main.py loads b, it goes via __init__.py which tries to load b.py before a.py. This means when b.py tries to load a it gets the module rather than the function - meaning you'll get the error message module object is not callable

The solution here is to swap the order in some_module/__init__.py:

from .a import a
from .b import b

Or, if this would create a circular dependency, change your file names to not match the functions, and load directly from the files rather than relying on __init__.py

Solution 12 - Python

A simple way to solve this problem is export the PYTHONPATH 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.

Solution 13 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser551717View Question on Stackoverflow
Solution 1 - PythonKatrielView Answer on Stackoverflow
Solution 2 - PythonBowen XuView Answer on Stackoverflow
Solution 3 - PythonJose AlbanView Answer on Stackoverflow
Solution 4 - PythonDeekshith AnandView Answer on Stackoverflow
Solution 5 - PythonzerocogView Answer on Stackoverflow
Solution 6 - PythonmoinudinView Answer on Stackoverflow
Solution 7 - PythonblackwindView Answer on Stackoverflow
Solution 8 - PythonLuke ExtonView Answer on Stackoverflow
Solution 9 - PythonNazmus Sakib AkashView Answer on Stackoverflow
Solution 10 - PythonumairhhhsView Answer on Stackoverflow
Solution 11 - PythonStuart MooreView Answer on Stackoverflow
Solution 12 - PythonOscar ArdilaView Answer on Stackoverflow
Solution 13 - PythonAyesha SiddiqaView Answer on Stackoverflow