简体   繁体   English

如何修复此错误“AttributeError:模块‘socket’没有属性‘gethostname’”

[英]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.

相关问题 如何修复错误'AttributeError:模块'board'没有属性'SCK'我正在使用树莓派 - How to fix the error 'AttributeError: module 'board' has no attribute 'SCK'' I am using raspberry pi 如何修复 AttributeError: 'module' 对象没有属性 'function'? - How to fix AttributeError: 'module' object has no attribute 'function'? 如何修复“AttributeError:‘module’对象没有属性‘x’”? - how to fix "AttributeError: 'module' object has no attribute 'x' "? 如何修复AttributeError:模块'testing2'没有属性'printPackage'? - How to fix AttributeError: module 'testing2' has no attribute 'printPackage'? 如何在wfastcgi中修复“ AttributeError:模块没有属性'wsgi'” - How to fix “AttributeError: module has no attribute 'wsgi' ” in wfastcgi 如何修复:AttributeError:模块“整洁”没有属性“配置” - How to fix: AttributeError: module 'neat' has no attribute 'config' 如何修复 AttributeError:模块“tensorflow”没有属性“ConfigProto” - How can I fix AttributeError: module 'tensorflow' has no attribute 'ConfigProto' 如何修复 AttributeError:模块“requests.sessions”没有属性“post” - How to Fix AttributeError: module 'requests.sessions' has no attribute 'post' 如何修复 AttributeError:模块“magpylib”没有属性“源” - How to Fix AttributeError: module 'magpylib" has no attribute 'source' 如何修复“AttributeError:模块'keras.backend'没有属性..” - How to fix "AttributeError: module 'keras.backend' has no attribute.."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM