Home >Backend Development >Python Tutorial >Why is my Python code throwing a \'TypeError: \'module\' object is not callable\' when creating a socket?
TypeError: 'module' Object Not Callable
The error "TypeError: 'module' object is not callable" occurs when your code attempts to invoke a module object as if it were a function. In the given scenario, the error arises due to the incorrect usage of the socket module.
Understanding the Issue
Socket, with a lowercase 's', is a module in Python that provides networking functionality. However, your code tries to create a socket using self.serv = socket(AF_INET,SOCK_STREAM). The problem is that self.serv references the module object socket, which is not callable. To create a socket, you need to import the socket class from the socket module.
Resolving the Error
To resolve the error, you have two options:
Use the Socket Class Directly: Import the socket class as follows:
import socket self.serv = socket.socket(AF_INET, SOCK_STREAM)
Use the from Statement: Import the socket class directly using the from statement:
from socket import socket self.serv = socket(AF_INET, SOCK_STREAM)
Explanation
The socket module contains a class named socket that defines the functionality for creating network sockets. By importing or directly referencing this class, your code can create and manipulate socket objects as needed.
The above is the detailed content of Why is my Python code throwing a \'TypeError: \'module\' object is not callable\' when creating a socket?. For more information, please follow other related articles on the PHP Chinese website!