microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.29k stars 1.18k forks source link

Virtual environment is not activated when using pyenv #23182

Closed melvinkokxw closed 4 months ago

melvinkokxw commented 5 months ago

Type: Bug

Behaviour

Virtual environment is not activated properly if pyenv setup script is added to ~/.zlogin. Running which python gives me /Users/<user>/.pyenv/shims/python instead of the expected .../.venv/bin/python

I found that the issue is only present if I add the pyenv setup script to ~/.zlogin. Adding it to ~/.zshrc and ~/.zprofile is ok.

I assume this is because activating pyenv inserts the path to its shims after VSCode activates the virtual environment. This causes calls to python to use the python in $HOME/.pyenv/shims/ instead of the virtual environment.

Steps to reproduce:

  1. Install pyenv
  2. Setup pyenv by adding init information to ~/.zlogin and ~/.zshrc
  3. Install a python version (pyenv install 3.9)
  4. Create a venv (python -m venv .venv)
  5. Open the folder in vscode code .
  6. Open a terminal and check the python executable used (which python)

Diagnostic data

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

``` 2024-04-05 12:48:15.457 [info] Experiment 'pythonRecommendTensorboardExt' is active 2024-04-05 12:48:15.457 [info] Experiment 'pythonREPLSmartSend' is active 2024-04-05 12:48:15.457 [info] Experiment 'pythonTerminalEnvVarActivation' is active 2024-04-05 12:48:15.457 [info] Experiment 'pythonTestAdapter' is active 2024-04-05 12:48:15.457 [info] Default formatter is set to null for workspace /Users//Projects/ 2024-04-05 12:48:15.457 [info] Test server listening. 2024-04-05 12:48:15.459 [info] Python interpreter path: ./.venv/bin/python 2024-04-05 12:48:15.465 [info] > pyenv which python 2024-04-05 12:48:15.465 [info] cwd: . 2024-04-05 12:48:17.884 [info] > . ./.venv/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python ~/.vscode/extensions/ms-python.python-2024.4.0/python_files/printEnvVariables.py 2024-04-05 12:48:17.884 [info] shell: zsh 2024-04-05 12:48:18.192 [info] > ~/.pyenv/versions/3.9.18/bin/python ~/.vscode/extensions/ms-python.python-2024.4.0/python_files/printEnvVariables.py 2024-04-05 12:48:18.192 [info] shell: zsh 2024-04-05 12:48:18.270 [info] Prepending environment variable PATH in collection with /Users//.vscode/extensions/ms-python.python-2024.4.0/python_files/deactivate/zsh:/Users//Projects//.venv/bin: {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-05 12:48:18.271 [info] Setting environment variable VIRTUAL_ENV in collection to /Users//Projects//.venv {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-05 12:48:18.271 [info] Prepending environment variable PS1 in collection with (.venv) {"applyAtShellIntegration":true,"applyAtProcessCreation":false} 2024-04-05 12:48:18.271 [info] Send text to terminal: /Users//.pyenv/versions/3.9.18/bin/python /Users//.vscode/extensions/ms-python.python-2024.4.0/python_files/printEnvVariablesToFile.py /Users//.vscode/extensions/ms-python.python-2024.4.0/python_files/deactivate/zsh/envVars.txt 2024-04-05 12:48:19.322 [info] Starting Pylance language server. ```

Extension version: 2024.4.0 VS Code version: Code 1.88.0 (Universal) (5c3e652f63e798a5ac2f31ffd0d863669328dc4c, 2024-04-03T13:28:18.899Z) OS version: Darwin arm64 23.4.0 Modes:

User Settings

``` languageServer: "Pylance" testing • pytestArgs: "" • pytestEnabled: true ```

Installed Extensions |Extension Name|Extension Id|Version| |---|---|---| |.gitignore Generator|piotrpalarz.vscode-gitignore-generator|1.0.3| |autoDocstring - Python Docstring Generator|njpwerner.autodocstring|0.6.1| |Dev Containers|ms-vscode-remote.remote-containers|0.354.0| |Docker|ms-azuretools.vscode-docker|1.29.0| |DotENV|mikestead.dotenv|1.0.1| |EditorConfig for VS Code|EditorConfig.EditorConfig|0.16.4| |Even Better TOML|tamasfe.even-better-toml|0.19.2| |Excel Viewer|GrapeCity.gc-excelviewer|4.2.59| |File Downloader|mindaro-dev.file-downloader|1.0.13| |Flake8|ms-python.flake8|2023.10.0| |Format Files|jbockle.jbockle-format-files|3.4.0| |Git Graph|mhutchie.git-graph|1.30.0| |Git History|donjayamanne.githistory|0.6.20| |GitHub Copilot|GitHub.copilot|1.178.0| |GitHub Copilot Chat|GitHub.copilot-chat|0.14.0| |GitLens — Git supercharged|eamodio.gitlens|14.9.0| |GitSweep|jellyedwards.gitsweep|1.0.0| |JavaScript Debugger|ms-vscode.js-debug|1.88.0| |JavaScript Debugger Companion Extension|ms-vscode.js-debug-companion|1.1.2| |Jupyter|ms-toolsai.jupyter|2024.3.0| |Jupyter Cell Tags|ms-toolsai.vscode-jupyter-cell-tags|0.1.9| |Jupyter Keymap|ms-toolsai.jupyter-keymap|1.1.2| |Jupyter Notebook Renderers|ms-toolsai.jupyter-renderers|1.0.17| |Jupyter Slide Show|ms-toolsai.vscode-jupyter-slideshow|0.1.6| |Markdown All in One|yzhang.markdown-all-in-one|3.6.2| |Material Icon Theme|PKief.material-icon-theme|4.34.0| |Material Theme|Equinusocio.vsc-material-theme|34.3.1| |Material Theme Icons|equinusocio.vsc-material-theme-icons|3.5.0| |Mypy Type Checker|ms-python.mypy-type-checker|2023.6.0| |Prettier SQL VSCode|inferrinizzard.prettier-sql-vscode|1.6.0| |Project Manager|alefragnani.project-manager|12.8.0| |Pylance|ms-python.vscode-pylance|2024.4.1| |Python|ms-python.python|2024.4.0| |Python Debugger|ms-python.debugpy|2024.4.0| |Remote - SSH|ms-vscode-remote.remote-ssh|0.110.1| |Remote - SSH: Editing Configuration Files|ms-vscode-remote.remote-ssh-edit|0.86.0| |Remote Development|ms-vscode-remote.vscode-remote-extensionpack|0.25.0| |Remote Explorer|ms-vscode.remote-explorer|0.4.3| |Ruff|charliermarsh.ruff|2024.16.0| |shell-format|foxundermoon.shell-format|7.2.5| |SQLite|alexcvzz.vscode-sqlite|0.14.1| |SQLite Viewer|qwtel.sqlite-viewer|0.3.13| |Table Visualizer for JavaScript Profiles|ms-vscode.vscode-js-profile-table|1.0.8| |vscode-icons|vscode-icons-team.vscode-icons|12.7.0| |vscode-pdf|tomoki1207.pdf|1.2.2| |WakaTime|WakaTime.vscode-wakatime|24.4.0| |WSL|ms-vscode-remote.remote-wsl|0.88.0| |YAML|redhat.vscode-yaml|1.14.0|
System Info |Item|Value| |---|---| |CPUs|Apple M2 (8 x 24)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|4, 3, 4| |Memory (System)|24.00GB (0.29GB free)| |Process Argv|--crash-reporter-id 56cf9e91-9035-4e63-b3e8-3f742295978d| |Screen Reader|no| |VM|0%|
A/B Experiments ``` vsliv368cf:30146710 vspor879:30202332 vspor708:30202333 vspor363:30204092 vscod805cf:30301675 binariesv615:30325510 vsaa593cf:30376535 py29gd2263:30899288 c4g48928:30535728 azure-dev_surveyone:30548225 962ge761:30959799 pythongtdpath:30769146 welcomedialogc:30910334 pythonidxpt:30866567 pythonnoceb:30805159 asynctok:30898717 pythontestfixt:30902429 pythonregdiag2:30936856 pyreplss1:30897532 pythonmypyd1:30879173 pythoncet0:30885854 h48ei257:31000450 pythontbext0:30879054 accentitlementst:30995554 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 cppperfnew:31000557 d34g3935:30971562 fegfb526:30981948 bg6jg535:30979843 ccp2r3:30993541 dsvsc020:30976470 pythonait:31006305 gee8j676:31009558 dsvsc021:30996838 gd77d436:30999572 vstestc:31010171 ```
anthonykim1 commented 5 months ago

Hello @melvinkokxw Thanks for filing the issue. What happens if you opt out of pythonTerminalEnvVarActivation? try adding: "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"]

anthonykim1 commented 5 months ago

Maybe a duplicate of: #23227

anthonykim1 commented 4 months ago

You should add to .zshrc since ~/.zlogin is sourced only during the start of login shell and the impact is pretty limited. Changes to .zlogin might affect the initial environment setup but wont affect further new shells. I highly recommend you stick to setting up your pyenv setup hooks in your shell initialization script which would be .zshrc in your case.