AttributeError:模块“os”没有属性“uname”

Jim*_*Lin 12 python operating-system

当我做:

>>> import os
>>> os.uname()
Run Code Online (Sandbox Code Playgroud)

我收到一个属性错误,如下所示:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    os.uname()
AttributeError: module 'os' has no attribute 'uname'
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题,因为我的 python 坏了或者是其他什么原因,因为在文档中。

小智 9

我在 Windows 10 上的 IDLE 中以完全相同的方式运行了您的代码,并得到了相同的结果。

>>> print(os.uname())
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print(os.uname())
AttributeError: module 'os' has no attribute 'uname'
Run Code Online (Sandbox Code Playgroud)

正如@Joran Beasley 指出的,此功能仅在某些操作系统中可用

来自在线编译器:

posix.uname_result(sysname='Linux', nodename='Check', release='5.4.10-x86_64-linode132', version='#1 SMP PREEMPT Thu Jan 9 21:17:12 UTC 2020', machine='x86_64')
Run Code Online (Sandbox Code Playgroud)

如果您想获取当前操作系统,我推荐该platform模块。

>>> import platform
>>> platform.platform()
'Windows-10-10.0.18362-SP0'
Run Code Online (Sandbox Code Playgroud)

有些人更喜欢使用os模块,但platform更具可读性。