我需要看些什么来确定我使用的是Windows还是Unix等等?
当前回答
我开始了一个更系统的列表,你可以使用各种模块(随意编辑和添加你的系统):
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())
其他回答
在windows 8上的有趣结果:
>>> import os
>>> os.name
'nt'
>>> import platform
>>> platform.system()
'Windows'
>>> platform.release()
'post2008Server'
编辑:这是一个bug
你也可以使用sys。平台,如果你已经导入了sys,你不想导入另一个模块
>>> import sys
>>> sys.platform
'linux2'
如果你不是在寻找内核版本等,而是在寻找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上运行时才能工作。如果您想要跨平台的通用脚本,您可以将此脚本与其他答案中给出的代码示例混合使用。
这个解决方案适用于python和jython。
模块os_identify.py:
import platform
import os
# This module contains functions to determine the basic type of
# OS we are running on.
# Contrary to the functions in the `os` and `platform` modules,
# these allow to identify the actual basic OS,
# no matter whether running on the `python` or `jython` interpreter.
def is_linux():
try:
platform.linux_distribution()
return True
except:
return False
def is_windows():
try:
platform.win32_ver()
return True
except:
return False
def is_mac():
try:
platform.mac_ver()
return True
except:
return False
def name():
if is_linux():
return "Linux"
elif is_windows():
return "Windows"
elif is_mac():
return "Mac"
else:
return "<unknown>"
像这样使用:
import os_identify
print "My OS: " + os_identify.name()
对于Jython,我发现获得操作系统名称的唯一方法是检查os.name Java属性(在WinXP上尝试使用sys, os和平台模块的Jython 2.5.3):
def get_os_platform():
"""return platform name, but for Jython it uses os.name Java property"""
ver = sys.platform.lower()
if ver.startswith('java'):
import java.lang
ver = java.lang.System.getProperty("os.name").lower()
print('platform: %s' % (ver))
return ver
推荐文章
- 如何在Flask-SQLAlchemy中按id删除记录
- 在Python中插入列表的第一个位置
- Python Pandas只合并某些列
- 如何在一行中连接两个集而不使用“|”
- 从字符串中移除前缀
- 代码结束时发出警报
- 如何在Python中按字母顺序排序字符串中的字母
- 在matplotlib中将y轴标签添加到次要y轴
- 如何消除数独方块的凹凸缺陷?
- 为什么出现这个UnboundLocalError(闭包)?
- 使用Python请求的异步请求
- 如何检查一个对象是否是python中的生成器对象?
- 如何从Python包内读取(静态)文件?
- 如何计算一个逻辑sigmoid函数在Python?
- python: SyntaxError: EOL扫描字符串文字