Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if python is being run in Ubuntu Linux

I have a Python 3.2 program that runs like this:

import platform
sysname = platform.system()
sysver = platform.release()
print(sysname+" "+sysver)

And on windows it returns:

Windows 7

But on Ubuntu and others it returns:
Linux 3.0.0-13-generic

I need something like:

Ubuntu 11.10 or Mint 12

like image 737
triunenature Avatar asked Dec 01 '11 09:12

triunenature


2 Answers

Looks like platform.dist() and platform.linux_distribution() are deprecated in Python 3.5 and will be removed in Python 3.8. The following works in Python 2/3

import platform
'ubuntu' in platform.platform().lower()

Example return value

>>> platform.platform()
'Linux-4.10.0-40-generic-x86_64-with-Ubuntu-16.04-xenial'
like image 51
crizCraig Avatar answered Sep 28 '22 06:09

crizCraig


The currently accepted answer uses a deprecated function. The proper way to do this as of Python 2.6 and later is:

import platform
print(platform.linux_distribution())

The documentation doesn't say if this function is available on non-Linux platforms, but on my local Windows desktop I get:

>>> import platform
>>> print(platform.linux_distribution())
('', '', '')

There's also this, to do something similar on Win32 machines:

>>> print(platform.win32_ver())
('post2008Server', '6.1.7601', 'SP1', 'Multiprocessor Free')
like image 45
unwind Avatar answered Sep 28 '22 06:09

unwind