Reputation: 670
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
Upvotes: 14
Views: 5879
Reputation: 1177
With platform
python module, I recommand uname()
method, which returns a NamedTuple:
import platform
print(platform.uname())
It returns something like:
uname_result(system='...', node='...', release='...', version='...', machine='...', processor='...')
version
element of tuple can be useful to have Ubuntu information.
Upvotes: 0
Reputation: 2712
Many of the solutions do not work when executing inside a Container (The result is the host distro instead.)
Less elegant - but container friendly approach:
from typing import Dict
def parse_env_file(path: str) -> Dict[str, str]:
with open(path, 'r') as f:
return dict(tuple(line.replace('\n', '').split('=')) for line in f.readlines() if not line.startswith('#'))
def is_ubuntu() -> bool:
return "ubuntu" in parse_env_file("/etc/os-release")["NAME"].lower())
Upvotes: 0
Reputation: 8897
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.version().lower()
Example return value
>>> platform.version()
'#45~20.04.1-Ubuntu SMP Mon Apr 4 09:38:31 UTC 2022'
Upvotes: 9
Reputation: 399833
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')
Upvotes: 6
Reputation: 245
is_ubuntu = 'ubuntu' in os.getenv('DESKTOP_SESSION', 'unknown')
Picks up if you are runnning in Unity or Unity-2D if that is what you are looking for.
Upvotes: -2
Reputation: 118530
Try platform.dist
.
>>> platform.dist()
('Ubuntu', '11.10', 'oneiric')
Upvotes: 5