user4708518
user4708518

Reputation:

Python unable to import socket

I am trying to make a IRC twitch bot, however I am runnning into an issue when importing socket

Whenever I run the program I get an error:

TypeError: 'module' object is not callable

Here is the traceback:

Traceback (most recent call last):
  File "C:\VERYLONGPATH\ChatBot\run.py", line 6, in <module>
    s = openSocket()
  File "C:\VERYLONGPATH\ChatBot\socket.py", line 5, in openSocket
    s = socket.socket()

And here is the actual python code:

run.py

from socket import openSocket
from initialize import joinRoom

s = openSocket()
joinRoom(s)

while True:
    presist = True

socket.py

import socket
from settings import HOST, PORT, PASS, ID, CHANNEL

def openSocket():
    s = socket.socket()
    s.connect((HOST, PORT))
    s.send("PASS " + PASS + "\r\n")
    s.send("NICK " + ID + "\r\n")
    s.send("JOIN #" + CHANNEL + "\r\n")
    return s

I am unsure what could be causing this error, as I am importing socket.

Upvotes: 0

Views: 2144

Answers (1)

Aaron Christiansen
Aaron Christiansen

Reputation: 11837

socket.py has the same name as another module, socket, so Python is getting confused as to which socket you mean when you do from socket import openSocket; it decides to import the module rather than your file. Python then throws this error because the socket module doesn't have an openSocket function.

To fix this, change the name of your socket.py file to something else, for example mysocket.py, and then change your code in run.py accordingly like this:

from mysocket import openSocket
...

Upvotes: 1

Related Questions