The standard library platform module is used to get information about the operating system on which Python is running and its version (release). Using this module, it is possible to switch the process for each OS and version.
The following information is provided here.
- Get the OS name:
platform.system()
- Get version (release) information:
platform.release()
,version()
- Get OS and version at once:
platform.platform()
- Examples of results for each OS
- macOS
- Windows
- Ubuntu
- Sample code to switch processing depending on OS
If you want to know the version of Python you are running, see the following article.
- Related Articles:Check and display the Python version (e.g. sys.version)
All sample code in the first half is run on macOS Mojave 10.14.2; example results on Windows and Ubuntu are shown in the second half; OS-specific functions are also discussed in the second half.
Get the OS name: platform.system()
The OS name is obtained by platform.system(). The return value is a string.
import platform
print(platform.system())
# Darwin
Get version (release) information: platform.release(), version()
The OS version (release) information is obtained with the following functions. In both cases, the return value is a string.
platform.release()
platform.version()
As shown in the following example, platform.release() returns simpler contents.
print(platform.release())
# 18.2.0
print(platform.version())
# Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64
Get OS and version at once: platform.platform()
The OS name and version (release) information can be obtained together using platform.platform(). The return value is a string.
print(platform.platform())
# Darwin-18.2.0-x86_64-i386-64bit
If the value of the argument terse is TRUE, only minimal information will be returned.
print(platform.platform(terse=True))
# Darwin-18.2.0
There is also an argument aliased.
print(platform.platform(aliased=True))
# Darwin-18.2.0-x86_64-i386-64bit
The result is the same in the example environment, but some operating systems will return an alias as the OS name.
If aliased is true, it returns the result using an alias instead of the common name of the system. For example, SunOS becomes Solaris.
platform.platform() — Access to underlying platform’s identifying data — Python 3.10.0 Documentation
Examples of results for each OS
Examples of results on macOS, Windows, and Ubuntu will be shown, as well as OS-specific functions.
macOS
Example of the result on macOS Mojave 10.14.2. Same as the example shown above.
print(platform.system())
# Darwin
print(platform.release())
# 18.2.0
print(platform.version())
# Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64
print(platform.platform())
# Darwin-18.2.0-x86_64-i386-64bit
Note that it is Darwin, not macOS or Mojave.
For more information about Darwin, see the Wikipedia page. There is also a description of the correspondence between the latest version number and the name in macOS.
There is a madOS-specific function called platform.mac_ver().
The return value is returned as a tuple (release, versioninfo, machine).
In the example environment, versioninfo is unknown and is an empty string tuple.
print(platform.mac_ver())
# ('10.14.2', ('', '', ''), 'x86_64')
Windows
Example of results on Windows 10 Home.
print(platform.system())
# Windows
print(platform.release())
# 10
print(platform.version())
# 10.0.17763
print(platform.platform())
# Windows-10-10.0.17763-SP0
Note that the return value 10 of platform.release() is a string, not an integer.
There is a Windows-specific function called platform.win32_ver().
The return value is returned as a tuple (release, version, csd, ptype).
csd indicates the status of the service pack.
print(platform.win32_ver())
# ('10', '10.0.17763', 'SP0', 'Multiprocessor Free')
Ubuntu
Example of the result on Ubuntu 18.04.1 LTS.
print(platform.system())
# Linux
print(platform.release())
# 4.15.0-42-generic
print(platform.version())
# #45-Ubuntu SMP Thu Nov 15 19:32:57 UTC 2018
print(platform.platform())
# Linux-4.15.0-44-generic-x86_64-with-Ubuntu-18.04-bionic
There is a Unix-specific function platform.linux_distribution().
The return value is returned as a tuple (distname, version, id).
print(platform.linux_distribution())
# ('Ubuntu', '18.04', 'bionic')
Note that platform.linux_distribution() has been removed in Python 3.8. It is recommended to use the third party library distro instead, which needs to be installed separately using pip.
Sample code to switch processing depending on OS
If you want to switch the function or method to be used depending on the OS, you can use a method such as platform.system() to determine the value.
The following is an example of obtaining the creation date of a file.
def creation_date(path_to_file):
"""
Try to get the date that a file was created, falling back to when it was
last modified if that isn't possible.
See http://stackoverflow.com/a/39501288/1709587 for explanation.
"""
if platform.system() == 'Windows':
return os.path.getctime(path_to_file)
else:
stat = os.stat(path_to_file)
try:
return stat.st_birthtime
except AttributeError:
# We're probably on Linux. No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
return stat.st_mtime
In this example, the value of platform.system() is first used to determine whether it is Windows or other.
Then, it further uses exception handling to switch the process between the case where the st_birthtime attribute exists and the other cases.