Python3 报错 dyld cache '/System/Library/dyld/dyld_shared_cache_x86_64h' not loaded: syscall to map cache into shared region failed

在执行 Python3 命令的时候报如下错误:

1
2
3
4
5
6
$ python3
dyld[44774]: dyld cache '/System/Library/dyld/dyld_shared_cache_x86_64h' not loaded: syscall to map cache into shared region failed
dyld[44774]: Library not loaded: /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
Referenced from: /Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python
Reason: tried: '/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation' (no such file), '/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation' (no such file)
[1] 44774 abort python3

估计是 Python 版本有问题,自 macOS Monterey 12.3 后,Python 2 已经从系统移除,安装命令行开发者工具后会自动安装 Python 3,那就可以换成 macOS 的 Python3 试试。

  1. 查询系统是否自带 Python 3,一般放在 /usr/bin 下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    $ l /usr/bin/ | grep -i python 
    lrwxr-xr-x 1 root wheel 74B Dec 8 2021 2to3- -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/2to3-2.7
    lrwxr-xr-x 1 root wheel 74B Dec 8 2021 2to3-2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/2to3-2.7
    lrwxr-xr-x 1 root wheel 73B Dec 8 2021 idle -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/idle2.7
    lrwxr-xr-x 1 root wheel 73B Dec 8 2021 idle2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/idle2.7
    lrwxr-xr-x 1 root wheel 74B Dec 8 2021 pydoc -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc2.7
    lrwxr-xr-x 1 root wheel 74B Dec 8 2021 pydoc2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc2.7
    lrwxr-xr-x 1 root wheel 75B Dec 8 2021 python -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
    lrwxr-xr-x 1 root wheel 82B Dec 8 2021 python-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
    lrwxr-xr-x 1 root wheel 75B Dec 8 2021 python2 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
    lrwxr-xr-x 1 root wheel 75B Dec 8 2021 python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
    lrwxr-xr-x 1 root wheel 82B Dec 8 2021 python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
    -rwxr-xr-x 1 root wheel 163K Dec 8 2021 python3
    lrwxr-xr-x 1 root wheel 76B Dec 8 2021 pythonw -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
    lrwxr-xr-x 1 root wheel 76B Dec 8 2021 pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
    lrwxr-xr-x 1 root wheel 77B Dec 8 2021 smtpd2.7.py -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/smtpd2.7.py
  2. 查看环境变量 PATH 下的 Python 3:

    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
    27
    28
    29
    30
    $ which python3
    /Library/Frameworks/Python.framework/Versions/3.6/bin/python3

    # ganzhixiong @ MacBook in ~ [12:19:27]
    $ echo $PATH | tr ':' '\n'
    /Users/ganzhixiong/development/flutter/bin/cache/dart-sdk/bin
    /Users/ganzhixiong/.dotnet/tools
    /Users/ganzhixiong/development/flutter/bin
    /Library/Frameworks/Python.framework/Versions/3.6/bin
    /usr/local/bin
    /usr/bin
    /bin
    /usr/sbin
    /sbin
    /usr/local/go/bin
    /usr/local/share/dotnet
    ~/.dotnet/tools
    /Library/Apple/usr/bin
    /Users/ganzhixiong/Library/Android/sdk/tools
    /Users/ganzhixiong/Library/Android/sdk/platform-tools
    /usr/local/mysql/bin
    /Users/ganzhixiong/Library/Android/sdk/build-tools/25.0.0/aapt
    /Users/ganzhixiong/.rvm/bin
    /Users/ganzhixiong/.rvm/bin

    # ganzhixiong @ MacBook in ~ [12:20:07]
    $ type -a python3
    python3 is /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
    python3 is /usr/local/bin/python3
    python3 is /usr/bin/python3
  3. 删除环境变量 PATH 中 /usr/bin 之前路径中的 Python 3:

    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
    27
    $ code ~/.bash_profile             

    # ganzhixiong @ MacBook in ~ [12:23:39]
    $ source ~/.bash_profile

    # ganzhixiong @ MacBook in ~ [12:24:55]
    $ type -a python3
    python3 is /usr/local/bin/python3
    python3 is /usr/bin/python3

    # ganzhixiong @ MacBook in ~ [12:26:27] C:134
    $ l /usr/local/bin/ | grep -i python3
    lrwxr-xr-x 1 ganzhixiong admin 61B Apr 21 11:00 python3 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
    lrwxr-xr-x 1 ganzhixiong admin 72B Jun 4 2018 python3-32 -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3-32
    lrwxr-xr-x 1 ganzhixiong admin 76B Jun 4 2018 python3-config -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3-config
    lrwxr-xr-x 1 ganzhixiong admin 71B Jun 4 2018 python3.6 -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
    lrwxr-xr-x 1 ganzhixiong admin 74B Jun 4 2018 python3.6-32 -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6-32
    lrwxr-xr-x 1 ganzhixiong admin 78B Jun 4 2018 python3.6-config -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6-config
    lrwxr-xr-x 1 ganzhixiong admin 72B Jun 4 2018 python3.6m -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m
    lrwxr-xr-x 1 ganzhixiong admin 79B Jun 4 2018 python3.6m-config -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config

    # ganzhixiong @ MacBook in ~ [12:26:42]
    $ rm /usr/local/bin/python3

    # ganzhixiong @ MacBook in ~ [12:27:08]
    $ type -a python3
    python3 is /usr/bin/python3
  4. 验证。

    1
    2
    3
    4
    5
    $ python3
    Python 3.8.9 (default, Oct 26 2021, 07:25:54)
    [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

参考

Python3 报错 dyld cache '/System/Library/dyld/dyld_shared_cache_x86_64h' not loaded: syscall to map cache into shared region failed

https://ganzhixiong.com/p/e1e0259b/

Author

干志雄

Posted on

2023-04-21

Updated on

2023-04-21

Licensed under

Comments