Pythonが実行されている環境のOSやバージョン情報などを取得
Pythonが実行されている環境のOSやそのバージョン(リリース)情報などを取得するには、標準ライブラリのplatform
モジュールを使う。これを利用するとOSやバージョンごとに処理を切り替えることが可能。
実行中のPythonのバージョンを知りたい場合は以下の記事を参照。
前半のサンプルコードはすべてmacOSで実行したもの。WindowsやUbuntuでの結果の例は後半で示している。OS固有の関数についても後半であわせて述べる。
本記事のサンプルコードでは以下のようにplatform
, os
, sys
モジュールをインポートしている。
import platform
import os
import sys
OS名を取得: platform.system(), os.name, sys.platform
OS名はplatform.system()
で取得する。返り値は文字列。
print(platform.system())
# Darwin
OS名はos.name
およびsys.platform
でも取得可能。os.name
は'posix'
(Unix系), 'nt'
(Windows), 'java'
のいずれか。sys.platform
は'darwin'
や'linux'
, 'win32'
などが返される。
print(os.name)
# posix
print(sys.platform)
# darwin
WindowsおよびUbuntuでの結果は後述。
バージョン(リリース)情報を取得: platform.release(), version()
OSのバージョン(リリース)情報はplatform.release()
およびplatform.version()
で取得する。どちらも返り値は文字列。
以下の例のようにplatform.release()
のほうがシンプルな内容が返される。
print(platform.release())
# 23.0.0
print(platform.version())
# Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:57 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8112
OSとバージョンをまとめて取得: platform.platform()
OS名およびバージョン(リリース)情報はplatform.platform()
でまとめて取得できる。返り値は文字列。
print(platform.platform())
# macOS-14.0-arm64-arm-64bit
引数terse
をTrue
とすると最小限の情報のみが返される。
print(platform.platform(terse=True))
# macOS-14.0
引数aliased
もある。例の環境では変わらないが、True
にするとOSによってはOS名として別名が返される場合がある。
print(platform.platform(aliased=True))
# macOS-14.0-arm64-arm-64bit
aliased が真なら、システムの名称として一般的な名称ではなく、別名を使用して結果を返します。たとえば、SunOS は Solaris となります。 platform.platform() — Python 3.12.0 ドキュメント
各OSでの結果の例
macOS, Windows, Ubuntuでの結果の例を示す。OS固有の関数についてもあわせて紹介する。
macOS
macOS Sonoma 14.0での結果の例。上で示した通り。
print(platform.system())
# Darwin
print(os.name)
# posix
print(sys.platform)
# darwin
print(platform.release())
# 23.0.0
print(platform.version())
# Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:57 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8112
print(platform.platform())
# macOS-14.0-arm64-arm-64bit
macOS
やSonoma
ではなくDarwin
となるので注意。DarwinについてはWikipediaのページなどを参照。最新のバージョン番号とmacOSにおける名称の対応は英語版を参照。
madOS固有の関数としてplatform.mac_ver()
がある。タプル(release, versioninfo, machine)
が返される。例の環境ではversioninfo
が不明で空文字列のタプルとなっている。
print(platform.mac_ver())
# ('14.0', ('', '', ''), 'arm64')
Windows
Windows 11 Homeでの結果の例。
print(platform.system())
# Windows
print(os.name)
# nt
print(sys.platform)
# win32
print(platform.release())
# 10
print(platform.version())
# 10.0.22621
print(platform.platform())
# Windows-10-10.0.22621-SP0
platform.release()
の返り値10
は整数ではなく文字列。Windows 11でもバージョン番号の先頭は10
なので注意。
Windows固有の関数としてplatform.win32_ver()
がある。タプル(release, version, csd, ptype)
が返される。csd
はサービスパックの状況を表す。
print(platform.win32_ver())
# ('10', '10.0.22621', 'SP0', 'Multiprocessor Free')
そのほか、エディションを文字列で返すplatform.win32_edition()
、IoTエディションのときにTrue
を返すplatform.win32_is_iot()
がPython 3.8で追加された。
print(platform.win32_edition())
# Core
print(platform.win32_is_iot())
# False
Ubuntu
Ubuntu 22.04.3 LTSでの結果の例。
print(platform.system())
# Linux
print(os.name)
# posix
print(sys.platform)
# linux
print(platform.release())
# 5.15.0-86-generic
print(platform.version())
# #96-Ubuntu SMP Wed Sep 20 08:23:49 UTC 2023
print(platform.platform())
# Linux-5.15.0-86-generic-x86_64-with-glibc2.35
Unix固有の関数としてディストリビューションの名前やバージョンを返すplatform.linux_distribution()
があったが、Python 3.8で削除された。
ディストリビューションの名前やバージョンはサードパーティライブラリdistroを使って取得できる。pip
などで別途インストールする必要がある。
- distro · PyPI
- python-distro/distro: A much more elaborate replacement for removed Python's
platform.linux_distribution()
method
import distro
print(distro.name())
# Ubuntu
print(distro.id())
# ubuntu
print(distro.version())
# 22.04
OSによって処理を切り替えるサンプルコード
OSによって使用する関数やメソッドを切り替えたい場合はplatform.system()
などの値を判定する方法がある。
ファイルの作成日時を取得する例を示す。
- 関連記事: Pythonでファイルのタイムスタンプ(作成日時や更新日時)を取得
- How to get file creation & modification date/times in Python? - Stack Overflow
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
この例では、まずplatform.system()
の値でWindowsかそれ以外かを判定し、さらに例外処理を利用してst_birthtime
属性が存在する場合とそれ以外で処理を切り替えている。
例外処理については以下の記事を参照。