Hamada Dz
Hamada Dz

Reputation: 21

How to fix this error "AttributeError: module 'socket' has no attribute 'gethostname'"

I am trying to execute this script to get the hostname of my laptop

I use Windows7 64bit and Python 3.6.4

I have tried this code in Python IDE and it worked . This error occurred in PyCharm

import socket
c = socket.gethostname()
print(c)

Do you have any solutions?

Upvotes: 2

Views: 3005

Answers (3)

Otavio Muraca
Otavio Muraca

Reputation: 1

I had the same problem when I used a file named "socket.py" so I renamed the file to another name and it's working now.

Upvotes: 0

Ashok Yadav
Ashok Yadav

Reputation: 1

When I was importing matplotlib.pyplot, this error was coming - AttributeError: module 'socket' has no attribute 'gethostname'

But I had created a file socket.py once ,I removed this file, this issue was fixed

Upvotes: 0

Cassie Xu
Cassie Xu

Reputation: 66

It's very likely that your file is named as socket.py.

Then when you import socket, it will import the current file instead of the python socket module.

Rename your file to another name and rm -rf socket.pyc (to remove the compiled bytecode generated by previous executions).

Upvotes: 2

Related Questions