'How do I start VS Code from the terminal when I get `./MacOS/Electron: No such file or directory`?

When I run code in the terminal I get the following error:

$ code .                                                                                                                                           

/Users/manuphatak/.pyenv/shims/python: line 21: /usr/local/Cellar/pyenv/1.2.21/libexec/pyenv: No such file or directory
/usr/local/bin/code: line 10: ./MacOS/Electron: No such file or directory

There's a related issue here: https://github.com/microsoft/vscode/issues/89037.

For the user in the issue, the problem magically solved itself, but how do I solve it?

I can open VS Code directly through the app. I've tried reinstalling the code command.

Diagnostics

$ cat "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"                                                                         

#!/usr/bin/env bash
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.

function realpath() { python -c "import os,sys; print(os.path.realpath(sys.argv[1]))" "$0"; }
CONTENTS="$(dirname "$(dirname "$(dirname "$(dirname "$(realpath "$0")")")")")"
ELECTRON="$CONTENTS/MacOS/Electron"
CLI="$CONTENTS/Resources/app/out/cli.js"
ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@"
exit $?
$ cat /Users/manuphatak/.pyenv/shims/python                                                                                                          

#!/usr/bin/env bash
set -e
[ -n "$PYENV_DEBUG" ] && set -x

program="${0##*/}"
if [[ "$program" = "python"* ]]; then
  for arg; do
    case "$arg" in
    -c* | -- ) break ;;
    */* )
      if [ -f "$arg" ]; then
        export PYENV_FILE_ARG="$arg"
        break
      fi
      ;;
    esac
  done
fi

export PYENV_ROOT="/Users/manuphatak/.pyenv"
exec "/usr/local/Cellar/pyenv/1.2.21/libexec/pyenv" exec "$program" "$@"
$ cat /usr/local/Cellar/pyenv/1.2.21/libexec/pyenv

cat: /usr/local/Cellar/pyenv/1.2.21/libexec/pyenv: No such file or directory
$ pyenv --version

pyenv 1.2.22


Solution 1:[1]

As seen in the line 6 of script (via cat `which code` ), it needs a functioning python executable.

In my case the project has a .python-version that pyenv did not have, and I get this error:

pyenv: version `3.10.0' is not installed (set by /Users/myname/projects/cool-proj/.python-version)
/usr/local/bin/code: line 10: ./MacOS/Electron: No such file or directory

And the solution is to do a pyenv install to get the version installed and code command will work fine again.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gerrytan