As the title says, how can I find the current operating system in python?
-
5Windows: `os.name == 'nt'`. Mac/Linux/BSD: `os.name == 'posix'`. – jeremysprofile Jan 04 '21 at 00:28
5 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, linux_distribution and dist seem to have gone from recent python versions, so they have a wrapper function here.
import platform
import sys
def linux_distribution():
try:
return platform.linux_distribution()
except:
return "N/A"
def 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(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')

- 9,316
- 1
- 42
- 48
-
3`platform.linux_distribution()` is depreciated, to be removed in 3.8. Consider changing to the [`distro`](https://pypi.org/project/distro/) package. See [here](https://docs.python.org/3.7/library/platform.html#platform.linux_distribution) for more details. – Jun 04 '19 at 02:23
-
@TylerGubala Makes sence that it is deprecated, since it was not available on all platforms to begin with. It was always spotty but I included it here, thank you for pointing this out. – Jens Timmerman Sep 09 '19 at 07:34
-
-
1@JensTimmerman But I upgraded from Windows 10 to Windows 11 a few months ago (using the free upgrade provided by Microsoft for eligible devices). But when I run `print(platform.system(), platform.release())`, it prints `Windows 10`. Can you tell me what the reason is and how do I resolve it? – NameError Apr 02 '22 at 13:22
-
@NameError sounds like this could be a bug in Windows, in python, or expected behaviour, I don't have access to a win10 or 11 machine right now so I can not test this, you could try opening an issue for python, the logich here doens't seem to include windows 11 yet: https://github.com/python/cpython/blob/main/Lib/platform.py – Jens Timmerman Apr 04 '22 at 10:56
-
FYI: AttributeError: module 'platform' has no attribute 'dist' MacOS 12.3.1 python_version 3.9.5 – JayRizzo Apr 15 '22 at 01:59
-
1@JayRizzo yep, seems like it's also gone on python 3.10 on my linux machine, I've updated my answer and also added int inside a wrapper function. – Jens Timmerman Apr 19 '22 at 14:53
-
Thanks @JensTimmerman! Also if anyone wants to get the `name of the MacOS` they are using I built this: https://stackoverflow.com/a/72722226/1896134 & also posted it on my github. – JayRizzo Jun 23 '22 at 22:21
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.

- 9,998
- 9
- 38
- 50

- 74,139
- 30
- 130
- 136
-
8I would not recommend either of these two. The more reliable way is `platform.system()`. See this answer: https://stackoverflow.com/a/58071295/207661. – Shital Shah Sep 23 '19 at 23:34
import os
print(os.name)
This gives you the essential information you will usually need. To distinguish between, say, different editions of Windows, you will have to use a platform-specific method.

- 3,234
- 3
- 33
- 49

- 951,095
- 183
- 1,149
- 1,285
-
40On the mac, os.name gives "posix", which for my case does not help - sys.platform did the trick – Steg Jul 27 '10 at 11:04
-
3
-
3
https://docs.python.org/library/os.html
To complement Greg's post, if you're on a posix system, which includes MacOS, Linux, Unix, etc. you can use os.uname() to get a better feel for what kind of system it is.

- 17,084
- 9
- 43
- 67

- 15,841
- 8
- 34
- 55
-
Although your answer was fist and was correct, Greg Hewgill's answer was more complete, I appreciate your answer and advise you to, post more then just links, in the future. – UnkwnTech Sep 21 '08 at 06:07
-
Yeah, it's the fastest gun in the west problem. I tend to post things quickly then edit with more info. – bmdhacks Sep 21 '08 at 06:09
-
I usually wait to answer my questions an I come back to them to see if there are any better posts even after I have accepted one. – UnkwnTech Sep 21 '08 at 06:17
Something along the lines:
import os
if os.name == "posix":
print(os.system("uname -a"))
# insert other possible OSes here
# ...
else:
print("unknown OS")