Zyther Crez
Zyther Crez

Reputation: 9

Unable to use "socket" in my vscode using python

I was just learning socket programming in python and was unable to run my socket commands in vs code.

the code i wrote is:-

import socket

import threading

PORT=5050

SERVER =socket.gethostbyname(socket.gethostname) 
print(SERVER)

The code produces this error:

Traceback (most recent call last):
  File "c:\Users\Gurkirat Singh\Desktop\tempCodeRunnerFile.py", line 1, in <module>
    import socket

  File "c:\Users\Gurkirat Singh\Desktop\socket.py", line 3, in <module>
    from xmlrpc.client import Server

  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\xmlrpc\client.py", line 136, in <module>
    import http.client

  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\http\client.py", 
line 789, in <module>
    class HTTPConnection:

  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\http\client.py", 
line 837, in HTTPConnection

    def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
AttributeError: partially initialized module 'socket' has no attribute '_GLOBAL_DEFAULT_TIMEOUT' (most likely due to a circular import)

Upvotes: 1

Views: 1508

Answers (1)

wkl
wkl

Reputation: 80031

Looking at your stack trace, you seem to have a file named socket.py:

File "c:\Users\Gurkirat Singh\Desktop\socket.py", line 3, in <module>
    from xmlrpc.client import Server

Having a file named socket.py in your workspace causes import problems because Python won't import its library module socket but instead will import your own file.

Rename your file from socket.py to something else like mysock.py and delete anything from your Desktop directory called socket.pyc if it exists.

Upvotes: 2

Related Questions