How to get the current OS name and version in Python

3 Answers

0 votes
import platform

print(platform.platform())



'''
run:

Linux-5.10.226-214.880.amzn2.x86_64-x86_64-with-glibc2.39

'''

 



answered Mar 27 by avibootz
0 votes
import platform

print(platform.system())
print(platform.release())
print(platform.version())


'''
run:

Linux
5.10.226-214.880.amzn2.x86_64
#1 SMP Tue Oct 8 16:18:15 UTC 2024

'''

 



answered Mar 27 by avibootz
0 votes
import platform
import sys

def platform_linux_distribution():
  try:
    return platform.linux_distribution()
  except:
    return "N/A"

def platform_dist():
  try:
    return platform.dist()
  except:
    return "N/A"

print("""Python version: %s
      dist: %s
      linux_distribution: %s
      system: %s
      machine: %s
      platform: %s
      uname: %s
      version: %s
      mac_ver: %s
      """ % (sys.version.split('\n'),
             str(platform_dist()),
             platform_linux_distribution(),
             platform.system(),
             platform.machine(),
             platform.platform(),
             platform.uname(),
             platform.version(),
             platform.mac_ver(),
))


'''
run:

Python version: ['3.12.3 (main, Sep 11 2024, 14:17:37) [GCC 13.2.0]']
      dist: N/A
      linux_distribution: N/A
      system: Linux
      machine: x86_64
      platform: Linux-5.10.226-214.880.amzn2.x86_64-x86_64-with-glibc2.39
      uname: uname_result(system='Linux', node='3cc5aefb89fd', release='5.10.226-214.880.amzn2.x86_64', version='#1 SMP Tue Oct 8 16:18:15 UTC 2024', machine='x86_64')
      version: #1 SMP Tue Oct 8 16:18:15 UTC 2024
      mac_ver: ('', ('', '', ''), '')

'''

 



answered Mar 27 by avibootz

Related questions

1 answer 34 views
1 answer 34 views
1 answer 30 views
1 answer 45 views
...