14

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

2
  • 3
    docs.python.org/library/… Commented Dec 1, 2011 at 9:05
  • Thanks, I really should read the docs more, ehh? Commented Dec 1, 2011 at 9:10

6 Answers 6

9

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'
Sign up to request clarification or add additional context in comments.

3 Comments

Works for Ubuntu 18.04 as well.
On my ubuntu 20.04 machine this value still doesn't contain ubuntu
@KeithSmiley I updated to work on Ubuntu 20, use platform.version() instead of platform.platform()
6

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')

1 Comment

5

Try platform.dist.

>>> platform.dist()
('Ubuntu', '11.10', 'oneiric')

5 Comments

platform.dist() is now out of date, we should be using platform.linux_distribution() (just read the docs, thanks to @Gazler)
@triunenature: No such deprecation warning exists in Python 3. docs.python.org/py3k/library/platform.html#platform.dist
Good to know :D I really need to read those docs more carefully.
According to the docs: docs.python.org/3/library/platform.html#platform.dist Either dist() or linux_distribution() is deprecated since 3.5, and will be removed in 3.7.
@DmitryVolosnykh: That's nice of them to point to an alternative.
0

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())

Comments

0

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.

Comments

-2
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.

2 Comments

What if you are running on a ubuntu server setup without a desktop?
The idea wasn't looking for ubuntu, its for mapping which OS is being run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.