Context: I am on Ubuntu 24.04.3 LTS and using Codex via the VSCode extension.
I am using pyenv to manage Python versions. Whenever Codex tries to run a shell script with bash -lc, it tries to initialize pyenv again.
As a result, the command fails after one minute with the following error:
pyenv: cannot rehash: /home/username/.pyenv/shims/.pyenv-shim exists
I also tried with the Codex CLI. It can run scripts successfully, but when running Python on the terminal, it gets a one-minute delay + error (but the command still runs… )
$ cat <<‘EOF’ >/tmp/simple_python.py
print(‘Hello from Python’)
EOF
cat <<‘EOF’ >/tmp/run_simple_python.sh
#!/usr/bin/env bash
python3 /tmp/simple_python.py
EOF
chmod +x /tmp/run_simple_python.sh
/tmp/run_simple_python.sh
pyenv: cannot rehash: /home/andras/.pyenv/shims/.pyenv-shim exists
Hello from Python
Is there a way to use Codex with pyenv?
cesar4
November 27, 2025, 4:52pm
2
I ran into this exact issue when using Codex inside VS Code.
Codex launches commands using bash -lc, which creates a login shell . In my case, that shell was re-initializing pyenv a second time. Since I had installed pyenv system-wide under /opt/pyenv (via /etc/profile.d/pyenv.sh), that file was always sourced again during bash -lc, causing:
pyenv: cannot rehash: /opt/pyenv/shims/.pyenv-shim exists
This made every Codex command hang for ~1 minute and then fail.
I fixed it by making pyenv initialization idempotent .
In /etc/profile.d/pyenv.sh, I wrapped the pyenv init lines with a guard:
# System-wide pyenv setup
export PYENV_ROOT="/opt/pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
# Guarded initialization: avoid running init twice in the same shell
if [ -z "$PYENV_INITIALIZED" ] && command -v pyenv >/dev/null 2>&1; then
export PYENV_INITIALIZED=1
# Path-related init (pyenv 2.x style)
eval "$(pyenv init --path)"
# Shell features (shims, shell integration)
eval "$(pyenv init -)"
fi
This avoids running pyenv init twice in the same shell.
After adding that guard, I deleted the stale lock file twice:
rm -f /opt/pyenv/shims/.pyenv-shim
pyenv rehash
After that, Codex worked normally inside VS Code again.