我平常會使用 pyenv 來管理 Python 版本,如何安裝與使用可以看我之前的文章

有次我在 WSL 想查看 Python 版本時,出現以下錯誤:

$ python
-bash: /mnt/c/Users/xxx/.pyenv/pyenv-win/shims/python: cannot execute: required file not found

搜尋後發現是因為 WSL 會把 Windows 的環境變數加到 Linux 的 $PATH 變數後面,而這台 WSL 還沒安裝 Python,導致執行 python 會搜尋到 Windows 的路徑導致報錯。

要禁止 WSL 自動加入 Windows 環境變數,需要修改 WSL 內的 /etc/wsl.conf,加入這段設定:

[interop]
appendWindowsPath=false

然後用 wsl --shutdown 關閉 WSL 再啟動就好了。

若有安裝多個 WSL distro,每台都要單獨設定。

參考連結