MacOS Python获取系统版本不对

背景

今天遇到个奇怪的问题,在conda安装软件,不能自动获取最新的版本,查看了一下info,发现里面获取的系统版本不对,开始怀疑是这个版本的问题,但是在deactive conda之后,获取的版本是正确的


系统版本

1
conda info

conda info
显示错误的版本

在conda外面打印版本,是✅的
于是代码查看了一下,Python 源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def mac_ver(release='', versioninfo=('', '', ''), machine=''):

""" Get macOS version information and return it as tuple (release,
versioninfo, machine) with versioninfo being a tuple (version,
dev_stage, non_release_version).

Entries which cannot be determined are set to the parameter values
which default to ''. All tuple entries are strings.
"""

# First try reading the information from an XML file which should
# always be present
info = _mac_ver_xml()
if info is not None:
return info

# If that also doesn't work return the default values
return release, versioninfo, machine

其实就是读取的 /System/Library/CoreServices/SystemVersion.plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

def _mac_ver_xml():
fn = '/System/Library/CoreServices/SystemVersion.plist'
if not os.path.exists(fn):
if 'SDKROOT' in os.environ:
fn = os.environ['SDKROOT'] + fn
if not os.path.exists(fn):
return None
else:
return None

try:
import plistlib
except ImportError:
return None

with open(fn, 'rb') as f:
pl = plistlib.load(f)
release = pl['ProductVersion']
versioninfo = ('', '', '')
machine = os.uname().machine
if machine in ('ppc', 'Power Macintosh'):
# Canonical name
machine = 'PowerPC'

return release, versioninfo, machine

对比了2个环境的SystemVersion.plist,就是同一个,cat 出来的版本不一样
conda deactive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ProductBuildVersion</key>
<string>22C65</string>
<key>ProductCopyright</key>
<string>1983-2022 Apple Inc.</string>
<key>ProductName</key>
<string>macOS</string>
<key>ProductUserVisibleVersion</key>
<string>13.1</string>
<key>ProductVersion</key>
<string>13.1</string>
<key>iOSSupportVersion</key>
<string>16.2</string>
</dict>
</plist>

conda active

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ProductBuildVersion</key>
<string>22C65</string>
<key>ProductCopyright</key>
<string>1983-2022 Apple Inc.</string>
<key>ProductName</key>
<string>Mac OS X</string>
<key>ProductUserVisibleVersion</key>
<string>10.16</string>
<key>ProductVersion</key>
<string>10.16</string>
<key>iOSSupportVersion</key>
<string>16.2</string>
</dict>
</plist>

都是cat的同一个路径文件,有点不理解了,都是同一个文件,为啥cat出来的内容不一样?简直反科学

同一个文件内容不一样?

网上查了下
https://stackoverflow.com/questions/69097567/macos-version-returned-as-10-16-instead-of-12-0
https://eclecticlight.co/2020/08/13/macos-version-numbering-isnt-so-simple/
大致是mac os考虑到,兼容性,在open的时候,hook掉了SystemVersion.plist,指向了 SystemVersionCompat.plis,同时留给了一个开关给我们控制,那就是环境变量SYSTEM_VERSION_COMPAT

解决办法

控制环境变量

1
export SYSTEM_VERSION_COMPAT = 0