tags:

views:

141

answers:

5

Hi guys, I have to get linux distro name from python script. There is dist method in platform module:

import platform
platform.dist()

But it returns

>>> platform.dist()
('', '', '')

Under my Arch Linux. Why? How can I get the name.

p.s. I have to check whether the distro is debian-based.


Upd: I found here python site, that dist() is deprecated since 2.6

>>> platform.linux_distribution()
('', '', '')
+1  A: 

Here's what I found:

platform.linux_distribution

Tries to determine the name of the Linux OS distribution name.

It says platform.dist is deprecated since 2.6

nc3b
A: 

Two options for ya.

import platform
platform.linux_distribution()
#something like ('Ubuntu', '9.10', 'karmic')

Or you could just read the contents of /etc/debian_version ("squeeze/sid") or /etc/lsb-release which would give:

DISTRIB_ID=Ubunt
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION="Ubuntu 9.10"

GL!

coffeetocode
+2  A: 

Works for me on Ubuntu:

('Ubuntu', '10.04', 'lucid')

I then used strace to find out what exactly the platform module is doing to find the distro 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

So, there is /etc/lsb-release containing this info, which comes from Ubuntu's Debian base-files package.

BjoernD
+2  A: 

Works here. And no, Arch Linux is not debian based.

>>> import platform
>>> platform.dist()
('SuSE', '11.2', 'x86_64')

So python does not know how to get archlinux release information and it has hardcoded looking for /etc/redhat-release and /etc/SuSE-release.

platform.dist() is an obsolete function. You should use platform.linux_distribution()

Actually, in my system it yields a different result:

>>> platform.linux_distribution()
('openSUSE ', '11.2', 'x86_64')

platform.linux_distribution() looks in /etc files containing "release" or "version" as string. It also looks in the standard LSB release file. If at the end that did not work, it resorts to a _dist_try_harder function which tries to get the information from other places.

So it is up to Arch Linux to provide a standard LSB release information or to patch python to use their "way".

duncan
A: 

You'll probably have to resort to:

if platform.linux_distribution() == ('', '', ''):
    # do something specific to look for Arch

or you could always augment lib/python2.6/platform.py and send in your changes.

msw