DEV Community

threadspeed
threadspeed

Posted on

4

OS detection in Python

Python can run on many platforms (Mac, Windows, Linux and others). Did you know it is super simple to detect the platform?

Python comes with the platform module that makes this easy. It is part of the Python standard library.

Start the Python shell and type this:

>>> import platform
>>> platform.architecture()
>>> platform.python_version()
>>> platform.uname()

An example run of the function calls:

>>> import platform
>>> platform.architecture()
('64bit', 'ELF')
>>> platform.python_version()
'3.7.7'
>>> platform.uname()
uname_result(system='Linux', node='localhost.localdomain', release='5.6.7-200.fc31.x86_64', version='#1 SMP Thu Apr 23 14:22:57 UTC 2020', machine='x86_64', processor='x86_64')
>>> 

The last call uname() returns a tuple. If you only want to know the operating system, you can do this:

import platform                                                                                                      

d = platform.uname()                                                                                                 
os = d[0]                                                                                                            
print(os)   

The uname function returns system, node, release, version, machine, and processor.

You can unpack the tuple like this:

import platform

d = platform.uname()
system, node, release, version, machine, cpu = d
print(system)
print(node)
print(release)
print(version)
print(machine)
print(cpu)

Related:

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay