检查Linux发行版名称 [英] Check Linux distribution name
问题描述
我必须从Python脚本获取Linux发行版名称.平台模块中有一个dist
方法:
I have to get the Linux distribution name from a Python script. There is a dist
method in the platform module:
import platform
platform.dist()
但是在我的Arch Linux下,它返回:
But under my Arch Linux it returns:
>>> platform.dist()
('', '', '')
为什么?我怎么得到名字?
Why? How can I get the name?
PS.我必须检查发行版是否基于Debian.
PS. I have to check whether the distribution is Debian-based.
更新:我在这里 Python网站,即dist()从2.6开始不推荐使用.
Update: I found here Python site, that dist() is deprecated since 2.6.
>>> platform.linux_distribution()
('', '', '')
推荐答案
这在Ubuntu上对我有效:
This works for me on Ubuntu:
('Ubuntu', '10.04', 'lucid')
然后我用strace
找出平台模块到底在做什么以查找发行版,这就是这一部分:
I then used strace
to find out what exactly the platform module is doing to find the distribution, and it is this part:
open("/etc/lsb-release", O_RDONLY|O_LARGEFILE) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=102, ...}) = 0
fstat64(3, {st_mode=S_IFREG|0644, st_size=102, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb76b1000
read(3, "DISTRIB_ID=Ubuntu\nDISTRIB_RELEAS"..., 8192) = 102
read(3, "", 4096) = 0
read(3, "", 8192) = 0
close(3) = 0
因此,有/etc/lsb-release
包含此信息,该信息来自Ubuntu的Debian基础文件包.
So, there is /etc/lsb-release
containing this information, which comes from Ubuntu's Debian base-files package.
这篇关于检查Linux发行版名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!