简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM