microsoft / vscode-python

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

Problems with new setting to remember Python interpreter for project #23350

Open tplobo opened 1 week ago

tplobo commented 1 week ago

Type: Bug

Behaviour & Steps to reproduce

I'm using mambaforge as an environment manager and run shells with zsh.

In my understanding one of the last updates made the "Python Interpreter" setting to be remembered and automatically retrieved when a project is opened. Which means the contents

# >>> conda initialize >>>
...
# <<< conda initialize <<<

are not needed in .zshrc anymore. This is nice, but it has created a few associated problems.

The first problem: this has modified some environment variables in terminals running an environment, for example:

(myenv) user@x86_64-apple-darwin13 > echo $HOST
x86_64-apple-darwin13...

instead of being equal to the output of hostname (opening vscode instance outside project folder):

user@computer > echo $HOST
computer

This is apparently a known issue with conda.

As a potential fix, I have tried to set a .vscode/.zshenv to enforce the proper value for HOST. But this is overwritten by the activation of the mamba environment, which happens after sourcing .zshenv. Here's the code:

# Fix `hostname` for conda environments
# See: https://github.com/conda/conda/issues/7031
echo "HOST=$HOST, hostname=$(hostname)"
if [[ "$HOST" == *"darwin"* ]]; then
    export HOST=$(scutil --get HostName)
    echo "(VSCode environment) Setting HOST to HostName: $HOST" 
fi

The second problem: to remedy that situation, I have migrated the lines in .zshenv to a .../activate.d/fix_host.sh file as a mamba post-activation script. However, due to VSCode automatically loading the Python Interpreter, the mamba environment is apparently never properly activated, so the post-activation scripts are never run. In fact, after opening VSCode in the project folder:

(myenv) user@x86_64-apple-darwin13 > mamba deactivate
Run 'mamba init' to be able to run mamba activate/deactivate
and start a new shell session. Or use conda to activate/deactivate.

and

(myenv) user@x86_64-apple-darwin13 > conda deactivate
CondaError: Run 'conda init' before 'conda deactivate'

so it looks like conda/mamba init are skipped.

Extension version: 2024.7.11241010 VS Code version: Code 1.89.0 (b58957e67ee1e712cebf466b995adf4c5307b2bd, 2024-05-01T02:10:10.196Z) OS version: Darwin x64 23.4.0 Modes:

User Settings

``` condaPath: "" languageServer: "Jedi" testing • pytestArgs: "" • pytestEnabled: true ```

Installed Extensions |Extension Name|Extension Id|Version| |---|---|---| |Active4D Theme|gerane.Theme-Active4D|0.0.5| |Amazon Q|amazonwebservices.amazon-q-vscode|1.1.0| |AWS Toolkit|amazonwebservices.aws-toolkit-vscode|3.1.0| |Eva Theme|fisheva.eva-theme|2.6.1| |GitHub Copilot|GitHub.copilot|1.186.0| |GitHub Copilot Chat|GitHub.copilot-chat|0.15.0| |GitHub Pull Requests|GitHub.vscode-pull-request-github|0.88.0| |GitLens — Git supercharged|eamodio.gitlens|14.9.1| |JavaScript Debugger|ms-vscode.js-debug|1.89.0| |JavaScript Debugger Companion Extension|ms-vscode.js-debug-companion|1.1.2| |JSON Tree View|ChaunceyKiwi.json-tree-view|2.6.0| |Jupyter|ms-toolsai.jupyter|2024.4.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| |Lua|sumneko.lua|3.8.3| |macOS Native Copy/Paste|qt-kaneko.macos-native-copy-paste|1.2.2| |Material Theme Icons|equinusocio.vsc-material-theme-icons|1.2.2| |PDF Preview|analytic-signal.preview-pdf|1.0.0| |Prettier - Code formatter|esbenp.prettier-vscode|10.4.0| |Property List Editor|ivhernandez.vscode-plist|0.1.3| |Python|ms-python.python|2024.7.11241010| |Python Debugger|ms-python.debugpy|2024.6.0| |python snippets|frhtylcn.pythonsnippets|1.0.2| |Restore Git Branch Tabs|gkotas.restore-git-branch-tabs|0.4.1| |Ruby LSP|Shopify.ruby-lsp|0.5.21| |Ruby Solargraph|castwide.solargraph|0.24.1| |Ruff|charliermarsh.ruff|2024.20.0| |Table Visualizer for JavaScript Profiles|ms-vscode.vscode-js-profile-table|1.0.9| |TIFF Preview|analytic-signal.preview-tiff|1.0.1| |TODO Highlight|wayou.vscode-todo-highlight|1.0.5| |VS Code Counter|uctakeoff.vscode-counter|3.4.0| |VSCode Great Icons|emmanuelbeziat.vscode-great-icons|2.1.104|
System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz (12 x 3200)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: disabled_off
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)|2, 2, 3| |Memory (System)|64.00GB (4.39GB free)| |Process Argv|--crash-reporter-id f2c256c7-7349-4739-8da9-43c6de0bfa57| |Screen Reader|no| |VM|0%|
A/B Experiments ``` vsliv368:30146709 vspor879:30202332 vspor708:30202333 vspor363:30204092 vswsl492:30256859 vscod805:30301674 binariesv615:30325510 vsaa593cf:30376535 py29gd2263:31024239 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 2e7ec940:31000449 pythontbext0:30879054 accentitlementsc:30995553 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 cppperfnew:31000557 ccp2r3:30993541 dsvsc020:30976470 pythonait:31006305 dsvsc021:30996838 0ee40948:31013168 pythoncenvpt:31022790 dwnewjupytercf:31035177 ```
tplobo commented 1 week ago

Edited the description to include more information on the commands in the shell scripts used to try to fix host variable.

anthonykim1 commented 1 week ago

Hello @tplobo, thanks for filing the issue. Can you try opting out of the terminal env var experiment by adding following code in your settings.json?

"python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"],

See if you can still repro the behavior afterwards.

tplobo commented 1 week ago

Hello @tplobo, thanks for filing the issue.

hey @anthonykim1, thanks for the reply

Can you try opting out of the terminal env var experiment by adding following code in your settings.json?

"python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"],

See if you can still repro the behavior afterwards.

Indeed, after adding your suggestion to the project .vscode/settings.json file, new terminals start with:

source /Users/user/mambaforge/bin/activate myenv
WARNING: overwriting environment variables set in the machine
overwriting variable ['LANG']
(my env) user@computer >

so the host in my prompt is corrected to the output of hostname. However, these commands return:

(my env) user@computer > echo $HOST
x86_64-apple-darwin13.4.0

(my env) user@computer > hostname
computer

so $HOST is still being overwritten by the environment activation, which seems to me that is the issue with conda that I reported previously. Not sure if this needs any fix, though.

Now, if to that setup I add a (project folder)/.vscode/.zshenv file, new terminals do not display any message echoed in that file (first lines in Terminal are the same as above), so it is apparently not being loaded. But if I instead add a ~/.vscode/.zshenv, messages from this file are echoed in new Terminal windows, i.e. it is being sourced; however $HOST and hostname are identical to the above.

Finally, if I remove both .zshenv files and add a myenv/.../activate.d/fix_host.sh file to my mambaforge directory, this file is clearly sourced because its messages are echoed in new Terminal windows. Buuut, $HOST and hostname are again identical to the above.