Python3 and 'code' CLI not working after updating to MacOS Monterey

Viewed 2633

I updated to MacOS Monterey and now python is not working:

➜  ~ python3 --version    
dyld[6578]: dyld cache '/System/Library/dyld/dyld_shared_cache_x86_64h' not loaded: syscall to map cache into shared region failed
dyld[6578]: 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]    6578 abort      python3 --version

But if I run:

➜  ~ /usr/bin/python3 --version
Python 3.8.9

I am able to run it. But when running code . for opening a project in vs code it gives the same error:

dyld[6683]: dyld cache '/System/Library/dyld/dyld_shared_cache_x86_64h' not loaded: syscall to map cache into shared region failed
dyld[6683]: 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)
/usr/local/bin/code: line 10: ./MacOS/Electron: No such file or directory

I am not sure what to do so that it runs commands like code . successfully.

2 Answers

Your python3 is 3.8.9 but the error message is for 3.6. It looks like you have clashing python versions. I fixed this error on my machine by uninstalling 3.6 and all of its syslinks like seen here under "Uninstalling Python 3 Using Terminal", except I used homebrew commands brew doctor and then brew cleanup for steps 4. and 5.

This is 100% a version conflict/PATH problem.

First, open up terminal and try running echo $PATH. It should print something like this:

/opt/homebrew/bin:/opt/homebrew/sbin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin

As you can see, it says 3.10 for me, while yours says 3.6

@guest_fish makes a good suggestion with deleting outdated versions of Python, but another way you can try is by checking the following:

  • vim $HOME/.zprofile ---> this is the zsh analogue to .bash_profile, here's what mine looks like:

# Setting PATH for Python 3.10
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
export PATH
eval "$(/opt/homebrew/bin/brew shellenv)"

Note how it matches the first part of the path I echo'd above

  • vim /etc/paths ---> this should match the middle parts of your PATH, mine looks like this:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
  • vim /etc/paths.d/<your_unique_id> (just press tab to complete it when you're in paths.d, you generally have only one option). Mine looks like this:

/Library/Apple/usr/bin

Note how it matches the end of the path

When you visit each of these files, you can modify them (password protected obv) to put /usr/bin/python3 first, meaning that will be the first place that it looks for python build files and will (hopefully) use the correct version


Now, to fix the code thing, you could try going into VSCode and hitting SHIFT + ⌘ + P, then select Shell Command: Install 'code' command in PATH - this should update it for you automatically.

Lmk how it goes!

Related