Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the current OS in Python? [duplicate]

Possible Duplicate:
Python: What OS am I running on?

As the title says, how can I find the current operating system in python?

like image 327
UnkwnTech Avatar asked Sep 21 '08 06:09

UnkwnTech


People also ask

How do I know if my python is Windows or Linux?

system() Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'. An empty string is returned if the value cannot be determined. If that isn't working, maybe try platform.

Where is OS module in Python?

The os module is a part of the standard library, or stdlib, within Python 3. This means that it comes with your Python installation, but you still must import it. All of the following code assumes you have os imported. Because it is not a built-in function, you must always import it.

How do I show the OS name in Python?

uname() method in python is used to get information about the current operating system. This method returns information like name, release, and version of the current operating system, name of the machine on the network, and hardware identifier in the form of attributes of a tuple-like object.


2 Answers

If you want user readable data but still detailed, you can use platform.platform()

>>> import platform >>> platform.platform() 'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne' 

platform also has some other useful methods:

>>> platform.system() 'Windows' >>> platform.release() 'XP' >>> platform.version() '5.1.2600' 

Here's a few different possible calls you can make to identify where you are

import platform import sys  def linux_distribution():   try:     return platform.linux_distribution()   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()), linux_distribution(), platform.system(), platform.machine(), platform.platform(), platform.uname(), platform.version(), platform.mac_ver(), )) 

The outputs of this script ran on a few different systems (Linux, Windows, Solaris, MacOS) and architectures (x86, x64, Itanium, power pc, sparc) is available here: https://github.com/hpcugent/easybuild/wiki/OS_flavor_name_version

e.g. Solaris on sparc gave:

Python version: ['2.6.4 (r264:75706, Aug  4 2010, 16:53:32) [C]'] dist: ('', '', '') linux_distribution: ('', '', '') system: SunOS machine: sun4u platform: SunOS-5.9-sun4u-sparc-32bit-ELF uname: ('SunOS', 'xxx', '5.9', 'Generic_122300-60', 'sun4u', 'sparc') version: Generic_122300-60 mac_ver: ('', ('', '', ''), '') 

or MacOS on M1

Python version: ['2.7.16 (default, Dec 21 2020, 23:00:36) ', '[GCC Apple LLVM 12.0.0 (clang-1200.0.30.4) [+internal-os, ptrauth-isa=sign+stri']  dist: ('', '', '')  linux_distribution: ('', '', '')  system: Darwin  machine: arm64  platform: Darwin-20.3.0-arm64-arm-64bit  uname: ('Darwin', 'Nautilus.local', '20.3.0', 'Darwin Kernel Version 20.3.0: Thu Jan 21 00:06:51 PST 2021; root:xnu-7195.81.3~1/RELEASE_ARM64_T8101', 'arm64', 'arm')  version: Darwin Kernel Version 20.3.0: Thu Jan 21 00:06:51 PST 2021; root:xnu-7195.81.3~1/RELEASE_ARM64_T8101  mac_ver: ('10.16', ('', '', ''), 'arm64') 
like image 135
Jens Timmerman Avatar answered Sep 28 '22 03:09

Jens Timmerman


I usually use sys.platform to get the platform. sys.platform will distinguish between linux, other unixes, and OS X, while os.name is "posix" for all of them.

For much more detailed information, use the platform module. This has cross-platform functions that will give you information on the machine architecture, OS and OS version, version of Python, etc. Also it has os-specific functions to get things like the particular linux distribution.

like image 33
dF. Avatar answered Sep 28 '22 03:09

dF.