[英]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我使用 Windows7 64 位和 Python 3.6.4
I have tried this code in Python IDE and it worked .我已经在 Python IDE 中尝试过这段代码,并且成功了。 This error occurred in PyCharm这个错误发生在 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
.您的文件很可能被命名为socket.py
。
Then when you import socket
, it will import the current file instead of the python socket module.然后当你import socket
,它会导入当前文件而不是 python socket 模块。
Rename your file to another name and rm -rf socket.pyc
(to remove the compiled bytecode generated by previous executions).将您的文件重命名为另一个名称和rm -rf socket.pyc
(以删除先前执行生成的已编译字节码)。
When I was importing matplotlib.pyplot, this error was coming - AttributeError: module 'socket' has no attribute 'gethostname'当我导入 matplotlib.pyplot 时,出现此错误 - AttributeError: module 'socket' has no attribute 'gethostname'
But I had created a file socket.py once ,I removed this file, this issue was fixed但是我已经创建了一个文件 socket.py 一次,我删除了这个文件,这个问题是固定的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.