VSCode/Language/Python/How To/Fix Stuck at Discovering Python Interpreters
- log 2024/Trimester/t3/w17/d3 - Wed, 2024/12/25 - debugging VSCode/Language/Python and VSCode/Extension/Python - VSCode/Proj/24t3/LearnVSCode/03 Basic Python
- 13:12 - I debugged this for over two hours today while working through 11:02 - ((2ca9c3c8-0810-4adc-ab6e-2a45fba87e46))
- I got stuck on configuring the interpreter after installing VSCode/Extension/Python. I tried everything. Here's what worked.
Problem Summary
- When I upgraded from Macbook/My to Macbook/My2, going from Chips/amd/intel64 to Chips/arm/M2, I had a bunch of old virtualenvs that pointed to versions of python that were broken on my machine. In addition, I had many old projects that used outdated, broken versions of python on my machine.
- When VSCode's python extensions start up, they try to introspect into the environment to see what virtualenvs are available. In order to support the widest market, the startup script searches for all the known and supported kinds of virtual environments. Unfortunately, it appears that if even one of the versions of python or one of the Py/Virtualenvs errors out during VS Code's startup, it breaks the whole environment discovery process.
- This is not generally well-known in the VS Code community, as far as I can tell. The only reason I figured this out was because of a comment that LazarusX left on the vscode-python github issue here:
It's migrated from my last machine, and only used a few years ago. I have no idea why it's scanned as a potential environment. However, IMO, there could be enhancement for this situation, because a broken environment should not block the entire interpreter discovery process. I also believe there is sort of regression here, because the extension used to work fine in spite of the broken environment.
Solution Summary
Key Technique - trace logs in the output panel of VS Code
- I saw that the VS Code team was very active about responding to bug reports, but they always needed a user to get the trace or debug python logs. This technique helped me figure out what my problem was.
Steps Taken
Uninstall any unneeded VSCode/Extensions including all python and jupyter extensions such as VSCode/Extension/Python
Destroy Pyenv/virtualenv associated with the project I was working on; Person/codekiln/GitHub/gitpa/acolyte
Reset PyPoetry
Reinstall Poetry using the official installer
- Poetry Installation
432 curl -sSL https://install.python-poetry.org | python3 -- Note that this script installs it into a virtualenv located in
~/Library/Application Support/pypoetry(where the config also is; see below)
Install PyPoetry/Shell/Zsh/OhMyZsh integration with Zsh/OhMyZsh
Reset Poetry Config
- I had blindly tried many different PyPoetry/Configurations (poetry configurations) while trying to debug this issue:
(gitp-acolyte) ✘ Me@Home ~/dev/gitpa/gitp-acolyte ↱ cc/04-host-n-dist/03-ai-ep-yml history | grep "poetry config" 112 poetry config virtualenvs.prefer-active-python true\n 385 poetry config virtualenvs.in-project true 395 poetry config virtualenvs.in-project false 447 poetry config virtualenvs.prefer-active-python true 456 poetry config --list- note that
poetry config virtualenvs.in-projectset to bothtrueandfalsewere advocated for here- python - VSCode doesn't show poetry virtualenvs in select interpreter option - Stack Overflow (asked 4 years ago, 188 votes up)
- I tried
virtualenvs.prefer-active-python truebecause of this confusing page in the Pyenv/Docs/Managing-Environments:- Managing environments | Documentation | Poetry - Python dependency management and packaging made easy
If you use a tool like pyenv to manage different Python versions, you can set the experimental
virtualenvs.prefer-active-pythonoption totrue. Poetry will then try to find the currentpythonof your shell.For instance, if your project requires a newer Python than is available with your system, a standard workflow would be:
- Ok, that definitely makes it seem like that's a good way to go for pyenv. However, what this actually does is not intuitive. And I ended up seeing some people who actually removed the configuration to solve it (which would have removed the default value of false)
poetry env infodoes not respectvirtualenvs.prefer-active-python = truesetting · Issue #5947 · python-poetry/poetry GitHub/python-poetry/poetry/Issue- Note, after all the steps below, my results were different from the OP here in that they were the same whether I ran
poetry run poetry env infoor if I ranpoetry env info(gitp-acolyte) Me@Home ~/dev/gitpa/gitp-acolyte ↱ cc/04-host-n-dist/03-ai-ep-yml poetry run poetry env info Virtualenv Python: 3.13.1 Implementation: CPython Path: /Users/Me/.pyenv/versions/3.13.1/envs/gitp-acolyte Executable: /Users/Me/.pyenv/versions/3.13.1/envs/gitp-acolyte/bin/python Valid: True Base Platform: darwin OS: posix Python: 3.13.1 Path: /Users/Me/.pyenv/versions/3.13.1 Executable: /Users/Me/.pyenv/versions/3.13.1/bin/python3.13 (gitp-acolyte) Me@Home ~/dev/gitpa/gitp-acolyte ↱ cc/04-host-n-dist/03-ai-ep-yml poetry env info Virtualenv Python: 3.13.1 Implementation: CPython Path: /Users/Me/.pyenv/versions/3.13.1/envs/gitp-acolyte Executable: /Users/Me/.pyenv/versions/3.13.1/envs/gitp-acolyte/bin/python Valid: True Base Platform: darwin OS: posix Python: 3.13.1 Path: /Users/Me/.pyenv/versions/3.13.1 Executable: /Users/Me/.pyenv/versions/3.13.1/bin/python3.13
- Note, after all the steps below, my results were different from the OP here in that they were the same whether I ran
- Managing environments | Documentation | Poetry - Python dependency management and packaging made easy
- note that
- These configurations are stored in a toml file; see Configuration | Poetry
- I read many articles and forum posts about using poetry with pyenv, and they all seemed to give contradictory advice. But the thing that ended up working for me was removing the customizations to the config that I did by removing this dir. In particular, I removed these customizations because
(gitp-acolyte) Me@Home ~/dev/gitpa/gitp-acolyte ↱ cc/04-host-n-dist/03-ai-ep-yml history | grep "pypoetry" 176 emulate bash -c '. /Users/Me/Library/Caches/pypoetry/virtualenvs/gitp-acolyte-kfI6rJB7-py3.13/bin/activate' 361 emulate bash -c '. /Users/Me/Library/Caches/pypoetry/virtualenvs/gitp-acolyte-kfI6rJB7-py3.13/bin/activate' 458 ls ~/Library/Application Support/pypoetry 459 ls "~/Library/Application Support/pypoetry" 460 ls ~/Library/Application\ Support/pypoetry 461 vim ~/Library/Application\ Support/pypoetry 462 rm -rf ~/Library/Application\ Support/pypoetry/config.toml
- These changes stuck (after the below)...
(gitp-acolyte) Me@Home ~/dev/gitpa/gitp-acolyte ↱ cc/04-host-n-dist/03-ai-ep-yml ls -la ~/Library/Application\ Support/pypoetry/ total 8 drwxr-xr-x@ 4 Me staff 128 Dec 25 12:48 . drwxr-xr-x 179 Me staff 5728 Dec 13 07:24 .. -rw-r--r--@ 1 Me staff 5 Dec 25 12:29 VERSION drwxr-xr-x@ 7 Me staff 224 Dec 25 12:29 venv
- I had blindly tried many different PyPoetry/Configurations (poetry configurations) while trying to debug this issue:
Recreate the Person/codekiln/GitHub/gitpa/acolyte Pyenv/virtualenv and specify the pyenv global version to be 3.13.1
Uninstall all unneeded python versions with Pyenv and their Pyenv/virtualenvs
- The first part of the solution was to uninstall all old virtual environments and old versions of python.
- shell
#!/bin/bash # Get list of virtualenvs in ~/.virtualenvs old_virtualenvs=$(ls ~/.virtualenvs) # Get list of pyenv-managed virtualenvs pyenv_virtualenvs=$(pyenv virtualenvs --bare) # Loop through each virtualenv in ~/.virtualenvs for env in $old_virtualenvs; do # Check if it's managed by pyenv-virtualenv if ! echo "$pyenv_virtualenvs" | grep -q "^$env$"; then echo "Deleting unused virtualenv: $env" rm -rf ~/.virtualenvs/"$env" fi done 473 pyenv versions 474 pyenv uninstall 3.5.6 475 pyenv uninstall 3.6.7 476 pyenv versions 477 pyenv uninstall 3.7.1 478 pyenv versions 479 pyenv uninstall 3.9.6 480 pyenv uninstall 3.12.0 481 pyenv versions 482 ls -la /Users/Me/.pyenv/versions/ 483 ls -la ~/.virtualenvs/ 485 touch scripts/clean_virtualenvs.sh 486 chmod +x scripts/clean_virtualenvs.sh 487 vscr scripts/clean_virtualenvs.sh 488 ./scripts/clean_virtualenvs.sh 490 ls -la /Users/Me/.virtualenvs/ 491 rm -rf ~/.virtualenvs/2023-11-26\ Sun\ Chiaroscurro\ Project 493 pwd 494 poetry install 495 poetry env info --path 496 poetry env info --executable | pbcopy
Reinitialize PyPoetry inside Person/codekiln/GitHub/gitpa/acolyte
Start VS Code and reinstall VSCode/Extension/Python; associate it with the pyenv virtualenv that poetry install installed to
- 495 poetry env info --path
496 poetry env info --executable | pbcopy
- now I did
Cmd Shift Pin #VSCode and didPython: Select Interpreterand then entered in the path to the virtualenv. It started working.
- 495 poetry env info --path