Python версия операционной системы

How to identify which OS Python is running on

The output of platform.system() is as follows:

@matth Slightly more consistent output. i.e. platform.system() returns «Windows» instead of «win32» . sys.platform also contains «linux2» on old versions of Python while it contains just «linux» on newer ones. platform.system() has always returned just «Linux» .

@baptistechéné, I know this has over an year since you asked, but as a comment won’t hurt, I’ll post it anyways 🙂 So, the reason behind it is because it shows the kernel name. The same way Linux (the kernel) distros have many names (Ubuntu, Arch, Fedora among others), but it’ll present itself as the kernel name, Linux. Darwin (a BSD-based Kernel), has its surrounding system, the macOS. I’m pretty sure apple did release Darwin as an open source code, but there’s no other distro running over Darwin that I know of.

@TooroSan os.uname() only exists for Unix systems. The Python 3 docs: docs.python.org/3/library/os.html Availability: recent flavors of Unix.

Here are the system results for Windows Vista!

>>> import os >>> os.name 'nt' >>> import platform >>> platform.system() 'Windows' >>> platform.release() 'Vista' 
>>> import os >>> os.name 'nt' >>> import platform >>> platform.system() 'Windows' >>> platform.release() '10' 

So, yeah, I just ran platform.release() on my Windows 10, and it definitely just gave me ‘8’ . Maybe I installed python before upgrading, but really??

I’d have thought it’s more likely you upgraded from Windows 8 (vs. it being a clean install) and whatever Python looks up in the registry or whatever was left behind?

The release lookup for python on Windows appears to use the Win32 api function GetVersionEx at its core. The notes at the top of this Microsoft article about that function could be relevant: msdn.microsoft.com/en-us/library/windows/desktop/…

For the record, here are the results on Mac:

>>> import os >>> os.name 'posix' >>> import platform >>> platform.system() 'Darwin' >>> platform.release() '8.11.1' 

19.2.0 is the release version of Darwin that comes with Catalina 10.15.2: en.wikipedia.org/wiki/MacOS_Catalina#Release_history

Sample code to differentiate operating systems using Python:

import sys if sys.platform.startswith("linux"): # could be "linux", "linux2", "linux3", . # linux elif sys.platform == "darwin": # MAC OS X elif os.name == "nt": # Windows, Cygwin, etc. (either 32-bit or 64-bit) 

Little problem: win64 does not exis: github.com/python/cpython/blob/master/Lib/platform.py. All Windows versions are win32 .

Читайте также:  Kotlin unit in java

Short Story

Use platform.system() . It returns Windows , Linux or Darwin (for OS X).

There are three ways to get the OS in Python, each with its own pro and cons:

>>> import sys >>> sys.platform 'win32' # could be 'linux', 'linux2, 'darwin', 'freebsd8' etc 

How this works (source): Internally it calls OS APIs to get the name of the OS as defined by the OS. See here for various OS-specific values.

Con: OS version dependent, so best not to use directly.

>>> import os >>> os.name 'nt' # for Linux and Mac it prints 'posix' 

How this works (source): Internally it checks if Python has OS-specific modules called posix or nt.

Pro: Simple to check if it is a POSIX OS

Con: no differentiation between Linux or OS X.

>>> import platform >>> platform.system() 'Windows' # For Linux it prints 'Linux'. For Mac, it prints `'Darwin' 

How this works (source): Internally it will eventually call internal OS APIs, get the OS version-specific name, like ‘win32’ or ‘win16’ or ‘linux1’ and then normalize to more generic names like ‘Windows’ or ‘Linux’ or ‘Darwin’ by applying several heuristics.

Pro: The best portable way for Windows, OS X, and Linux.

Con: Python folks must keep normalization heuristic up to date.

  • If you want to check if OS is Windows or Linux, or OS X, then the most reliable way is platform.system() .
  • If you want to make OS-specific calls, but via built-in Python modules posix or nt , then use os.name .
  • If you want to get the raw OS name as supplied by the OS itself, then use sys.platform .

So much for «There should be one (and preferably only one) way to do things». However I believe this is the right answer. You would need to compare with titled OS names but it’s not such an issue and will be more portable.

Note that in Python 3.10 , platform.system defaults to sys.platform if the os.uname throws an exception: github.com/python/cpython/blob/…

I started a bit more systematic listing of what values you can expect using the various modules:

Linux (64 bit) + WSL

 x86_64 aarch64 ------ ------- os.name posix posix sys.platform linux linux platform.system() Linux Linux sysconfig.get_platform() linux-x86_64 linux-aarch64 platform.machine() x86_64 aarch64 platform.architecture() ('64bit', '') ('64bit', 'ELF') 
  • I tried with Arch Linux and Linux Mint, but I got the same results
  • on Python 2, sys.platform is suffixed by the kernel version, e.g., linux2 , and everything else stays identical
  • the same output on Windows Subsystem for Linux (I tried with Ubuntu 18.04 (Bionic Beaver) LTS), except platform.architecture() = (’64bit’, ‘ELF’)
Читайте также:  Java util functions predicate

Windows (64 bit)

(with 32-bit column running in the 32-bit subsystem)

Official Python installer 64 bit 32 bit ------------------------- ----- ----- os.name nt nt sys.platform win32 win32 platform.system() Windows Windows sysconfig.get_platform() win-amd64 win32 platform.machine() AMD64 AMD64 platform.architecture() ('64bit', 'WindowsPE') ('64bit', 'WindowsPE') msys2 64 bit 32 bit ----- ----- ----- os.name posix posix sys.platform msys msys platform.system() MSYS_NT-10.0 MSYS_NT-10.0-WOW sysconfig.get_platform() msys-2.11.2-x86_64 msys-2.11.2-i686 platform.machine() x86_64 i686 platform.architecture() ('64bit', 'WindowsPE') ('32bit', 'WindowsPE') msys2 mingw-w64-x86_64-python3 mingw-w64-i686-python3 ----- ------------------------ ---------------------- os.name nt nt sys.platform win32 win32 platform.system() Windows Windows sysconfig.get_platform() mingw mingw platform.machine() AMD64 AMD64 platform.architecture() ('64bit', 'WindowsPE') ('32bit', 'WindowsPE') Cygwin 64 bit 32 bit ------ ----- ----- os.name posix posix sys.platform cygwin cygwin platform.system() CYGWIN_NT-10.0 CYGWIN_NT-10.0-WOW sysconfig.get_platform() cygwin-3.0.1-x86_64 cygwin-3.0.1-i686 platform.machine() x86_64 i686 platform.architecture() ('64bit', 'WindowsPE') ('32bit', 'WindowsPE') 
  • there is also distutils.util.get_platform() which is identical to `sysconfig.get_platform
  • Anaconda on Windows is the same as the official Python Windows installer
  • I don’t have a Mac nor a true 32-bit system and was not motivated to do it online

To compare with your system, simply run this script:

from __future__ import print_function import os import sys import platform import sysconfig print("os.name ", os.name) print("sys.platform ", sys.platform) print("platform.system() ", platform.system()) print("sysconfig.get_platform() ", sysconfig.get_platform()) print("platform.machine() ", platform.machine()) print("platform.architecture() ", platform.architecture()) 

Источник

How do I check the operating system in Python?

I want to check the operating system (on the computer where the script runs). I know I can use os.system(‘uname -o’) in Linux, but it gives me a message in the console, and I want to write to a variable. It will be okay if the script can tell if it is Mac, Windows or Linux. How can I check it?

5 Answers 5

from sys import platform if platform == "linux" or platform == "linux2": # linux elif platform == "darwin": # OS X elif platform == "win32": # Windows. 

sys.platform has finer granularity than sys.name .

For the valid values, consult the documentation.

Note that since Python 3.3, «linux2» is no longer a possible value of platform (see the linked docs for corroboration) and so if you only need to support Python 3.3 and later you can safely delete the ` or platform == «linux2″` clause from the first condition.

If you want to know on which platform you are on out of «Linux», «Windows», or «Darwin» (Mac), without more precision, you should use:

>>> import platform >>> platform.system() 'Linux' # or 'Windows'/'Darwin' 

The platform.system function uses uname internally.

Читайте также:  Есть ли смысл учить python

I like this solution but I want to point out that from the docs it states that it will return Linux , Windows , Java or an empty string.devdocs.io/python~3.7/library/platform#platform.system

@BrandonBenefield, the enumeration is an example of possible values. On Apple devices, it returns “Darwin”.

You can get a pretty coarse idea of the OS you’re using by checking sys.platform .

Once you have that information you can use it to determine if calling something like os.uname() is appropriate to gather more specific information. You could also use something like Python System Information on unix-like OSes, or pywin32 for Windows.

There’s also psutil if you want to do more in-depth inspection without wanting to care about the OS.

os.uname() returns an error on windows: >>> os.uname() Traceback (most recent call last): File ««, line 1, in AttributeError: module ‘os’ has no attribute ‘uname’. Did you mean: ‘name’?

Источник

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

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

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: ('', ('', '', ''), '') 
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') 

Источник

Оцените статью