我需要看些什么来确定我使用的是Windows还是Unix等等?
当前回答
如果你想要用户可读的数据但仍然是详细的,你可以使用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, power pc, sparc)上,可在这里获得:https://github.com/hpcugent/easybuild/wiki/OS_flavor_name_version
Ubuntu 12.04服务器为例给出:
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: ('', ('', '', ''), '')
其他回答
我开始了一个更系统的列表,你可以使用各种模块(随意编辑和添加你的系统):
Linux (64bit) + 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')
用archlinux和mint尝试了一下,得到了同样的结果 python2 sys. exe平台的后缀是内核版本,例如linux2,其他的都是相同的 在Windows子系统for Linux上输出相同(尝试使用ubuntu 18.04 LTS),除了platform.architecture() = ('64bit', 'ELF')
窗口(64位)
(在32位子系统中运行32位列)
official python installer 64bit 32bit
------------------------- ----- -----
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 64bit 32bit
----- ----- -----
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 64bit 32bit
------ ----- -----
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 '相同 Windows上的Anaconda与官方的python Windows安装程序相同 我没有Mac电脑,也没有真正的32位系统,也没有上网的动力
要与您的系统进行比较,只需运行这个脚本(如果缺少结果,请在这里附加:)
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())
我知道这是一个老问题,但我相信我的答案可能会对那些正在寻找一种简单易懂的python方法来检测代码中的操作系统的人有所帮助。在python3.7上测试
from sys import platform
class UnsupportedPlatform(Exception):
pass
if "linux" in platform:
print("linux")
elif "darwin" in platform:
print("mac")
elif "win" in platform:
print("windows")
else:
raise UnsupportedPlatform
像下面这样一个简单的Enum实现怎么样?不需要外部库!
import platform
from enum import Enum
class OS(Enum):
def checkPlatform(osName):
return osName.lower()== platform.system().lower()
MAC = checkPlatform("darwin")
LINUX = checkPlatform("linux")
WINDOWS = checkPlatform("windows") #I haven't test this one
简单地,您可以使用Enum值访问
if OS.LINUX.value:
print("Cool it is Linux")
附注:它是python3
如果你不是在寻找内核版本等,而是在寻找linux发行版,你可能会想要使用以下内容
在python2.6 +
>>> import platform
>>> print platform.linux_distribution()
('CentOS Linux', '6.0', 'Final')
>>> print platform.linux_distribution()[0]
CentOS Linux
>>> print platform.linux_distribution()[1]
6.0
在python2.4
>>> import platform
>>> print platform.dist()
('centos', '6.0', 'Final')
>>> print platform.dist()[0]
centos
>>> print platform.dist()[1]
6.0
显然,这只有在linux上运行时才能工作。如果您想要跨平台的通用脚本,您可以将此脚本与其他答案中给出的代码示例混合使用。
您可以查看pip-date包的一部分pyOSinfo中的代码,以获得最相关的操作系统信息,就像您的Python发行版所看到的那样。
人们检查操作系统最常见的原因之一是终端兼容性,以及某些系统命令是否可用。不幸的是,这种检查是否成功在一定程度上取决于你的python安装和操作系统。例如,uname在大多数Windows python包中是不可用的。上面的python程序将向您展示最常用的内置函数的输出,这些函数已经由os, sys, platform, site提供。
因此,只获得基本代码的最好方法是将其视为示例。(我想我可以直接粘贴在这里,但这样做就不政治正确了。)