>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.22-15-generic'
platform.system()
的输出如下:
Linux
Darwin
Windows
platform
而不是 sys.platform
? - matthplatform.system()
返回 "Windows"
而不是 "win32"
。在旧版本的 Python 中,sys.platform
还包含 "linux2"
,而在较新的版本中则只包含 "linux"
。而 platform.system()
始终只返回 "Linux"
。 - erbos.uname()
只存在于 Unix 系统中。Python 3 文档链接如下:
https://docs.python.org/3/library/os.html
可用性:最近的 Unix 系统。
- Irving Moy以下是Windows Vista的系统结果!
>>> import os
>>> os.name
'nt'
>>> import platform
>>> platform.system()
'Windows'
>>> platform.release()
'Vista'
而对于Windows 10:
>>> import os
>>> os.name
'nt'
>>> import platform
>>> platform.system()
'Windows'
>>> platform.release()
'10'
platform.release()
的翻译是'7'
。 - Hugoplatform.release()
,结果显示为“'8'”。也许是因为我在升级系统前就已经安装了Python,但是这样真的没问题吗? - Codesmith仅供参考,以下是在 Mac 上的结果:
>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Darwin'
>>> platform.release()
'8.11.1'
platform.release()
返回 '19.2.0'
。 - user3064538短故事
使用 platform.system()
。它会返回 Windows
、Linux
或 Darwin
(用于 OS X)。
长故事
在 Python 中获取操作系统有三种方法,每种方法都有其优缺点:
方法 1
>>> import sys
>>> sys.platform
'win32' # could be 'linux', 'linux2, 'darwin', 'freebsd8' etc
这是如何工作的(source):它在内部调用操作系统API来获取由操作系统定义的操作系统名称。请参见here以获取各种特定于操作系统的值。
优点:没有魔法,低级别。
缺点:依赖于操作系统版本,因此最好不要直接使用。
方法2
>>> import os
>>> os.name
'nt' # for Linux and Mac it prints 'posix'
这是如何工作的(source):内部检查Python是否具有名为posix或nt的特定于操作系统的模块。
优点:简单检查是否为POSIX操作系统
缺点:无法区分Linux或OS X。
方法3
>>> import platform
>>> platform.system()
'Windows' # For Linux it prints 'Linux'. For Mac, it prints `'Darwin'
这是如何运作的(source):内部最终会调用操作系统API,获取操作系统特定版本名称,例如“win32”或“win16”或“linux1”,然后通过应用多个启发式算法来规范化为更通用的名称,例如“Windows”或“Linux”或“Darwin”。
优点:适用于Windows、OS X和Linux的最佳可移植方式。
缺点:Python开发者必须保持规范化启发式算法的最新状态。
摘要
platform.system()
。posix
或nt
进行操作系统特定的调用,则使用os.name
。sys.platform
。os.uname
抛出异常,则platform.system
默认为sys.platform
:https://github.com/python/cpython/blob/a6c3f6d34792c5f9dc296ce8329938b7b2b02ea9/Lib/platform.py#L830-L833 - Heberto Mayorquin使用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)
sys.platform == 'cygwin'
。 - Ed Randallwin64
不存在:https://github.com/python/cpython/blob/master/Lib/platform.py。所有 Windows 版本都是 win32
。 - user136036我开始更系统地列出了使用各种模块可以期望的值:
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')
sys.platform
后缀为内核版本,例如linux2
,其他所有内容保持不变platform.architecture() = ('64bit','ELF')
(在32位子系统中运行32位列)
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')
distutils.util.get_platform()
,它与`sysconfig.get_platform`相同要与您的系统进行比较,只需运行此脚本:
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())
sys.platform
为win32
,就像你所报告的那样,但是MSYS2和UCRT64报告cygwin
而不是msys
。 - Paebbels如果您已经导入了sys
并且不想再导入另一个模块,您还可以使用sys.platform
>>> import sys
>>> sys.platform
'linux2'
如果您想要用户可读的数据,但仍然详细,可以使用platform.platform():
>>> import platform
>>> platform.platform()
'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne'
以下是几个不同的可能调用,可以用来确定您所在的位置。linux_distribution和dist在最近的Python版本中已被删除。
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(),
))
这个脚本的输出在几个不同的系统(Linux、Windows、Solaris 和 macOS)和架构(x86、x64、Itanium、PowerPC 和 SPARC)上运行,并且可以在 OS_flavor_name_version 上获取。
例如,Ubuntu 12.04 服务器(Precise Pangolin)会给出:
Python version: ['2.6.5 (r265:79063, Oct 1 2012, 22:04:36) ', '[GCC 4.4.3]']
dist: ('Ubuntu', '10.04', 'lucid')
linux_distribution: ('Ubuntu', '10.04', 'lucid')
system: Linux
machine: x86_64
platform: Linux-2.6.32-32-server-x86_64-with-Ubuntu-10.04-lucid
uname: ('Linux', 'xxx', '2.6.32-32-server', '#62-Ubuntu SMP Wed Apr 20 22:07:43 UTC 2011', 'x86_64', '')
version: #62-Ubuntu SMP Wed Apr 20 22:07:43 UTC 2011
mac_ver: ('', ('', '', ''), '')
DeprecationWarning: dist() and linux_distribution() functions are deprecated in Python 3.5
- user3064538使用:
import psutil
psutil.MACOS # True ("OSX" is deprecated)
psutil.WINDOWS # False
psutil.LINUX # False
如果我使用的是 macOS,那么这将是输出结果。
返回系统/操作系统名称,例如“Linux”,“Darwin”,“Java”,“Windows”。如果无法确定该值,则返回空字符串。
import platform
system = platform.system().lower()
is_windows = system == 'windows'
is_linux = system == 'linux'
is_mac = system == 'darwin'
Arch
? - dio