microsoft / vscode-python

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

VSCode does not properly load env vars for environment created with micromamba executable #24490

Open AmerM137 opened 4 days ago

AmerM137 commented 4 days ago

Environment data

Notes: I alias micromamba to mamba, so anywhere where it says mamba can be replaced with micromamba. I am using the stand-alone executable for micromamba. I've also replaced personal information with XXX where applicable.

Code Snippet

import os
import pandas as pd
from sqlalchemy import URL, create_engine

database = "somedatabase"
user = "myuser"
password = os.environ["somedatabase_password"]

url = URL.create(
    "postgresql+psycopg",
    username=user,
    password=password,
    host="localhost",
    port=12345,
    database=somedatabase,
)

engine = create_engine(url)
q = "select * from some_table"
df = pd.read_sql(q, con=engine)

ERROR->kernel crashes immediately

Repro Steps

  1. Without activating an environment created with micromamba, launch vscode and attempt to connect to a remote postgres instance. The kernel crashes as soon data is queried from the database.
  2. If the environment created with micromamba is activated then VSCode is launched with "code" from the cli, the above problem disappears. It looks like it's not loading the correct environment variables for the venv.
  3. Out of coincidence, I noticed something weird. I installed miniforge alongside the micromamba executable, and all of a sudden the problem stated in above disappears! Everything works even if VSCode is launched without activating the environment beforehand. See list of environment variables. There is some weird mixing of miniforge3 and micromamba env vars which should not happen.
  4. There are no issues whatsoever if the code is ran from native jupyter notebook, jupyter-lab or terminal IPython. VSCode jupyter is the only environment where this happens.

Supporting Info

#1 Micromamba env not activated beforehand - Notice that there is only one conda-related env var available. There are a few env vars and site packages related errors in the jupyter log.

envvars
CONDA_PREFIX=C:\Users\XXX\mamba\envs\sticks

Visual Studio Code (1.95.3, undefined, desktop)
Jupyter Extension Version: 2024.10.0.
Python Extension Version: 2024.20.0.
Pylance Extension Version: 2024.11.3.
Platform: win32 (x64).
Temp Storage folder ~\AppData\Roaming\Code\User\globalStorage\ms-toolsai.jupyter\version-2024.10.0
Workspace folder ~\Desktop\XXX\XXX, Home = c:\Users\XXX
14:55:00.325 [info] Starting Kernel (Python Path: ~\mamba\envs\sticks\python.exe, Conda, 3.12.7) for ~\Desktop\XXX\XXX\notebooks\check_etl.ipynb (disableUI=true)
14:55:00.471 [error] Failed to get activated conda env vars for ~\mamba\envs\sticks\python.exe
                 in 147ms
14:55:00.472 [error] Unable to determine site packages path for python ~\mamba\envs\sticks\python.exe (Conda)
14:55:00.476 [info] Process Execution: ~\mamba\envs\sticks\python.exe -m pip list
14:55:00.477 [error] Failed to get activated conda env vars for ~\mamba\envs\sticks\python.exe
                 in 138ms
14:55:00.478 [error] Unable to determine site packages path for python ~\mamba\envs\sticks\python.exe (Conda)
14:55:00.482 [info] Process Execution: ~\mamba\envs\sticks\python.exe -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
14:55:00.486 [info] Process Execution: ~\mamba\envs\sticks\python.exe c:\Users\~\.vscode\extensions\ms-toolsai.jupyter-2024.10.0-win32-x64\pythonFiles\vscode_datascience_helpers\kernel_interrupt_daemon.py --ppid 11500
    > cwd: ~\.vscode\extensions\ms-toolsai.jupyter-2024.10.0-win32-x64\pythonFiles\vscode_datascience_helpers
14:55:00.576 [info] Process Execution: ~\mamba\envs\sticks\python.exe -m ipykernel_launcher --f=c:\Users\~\AppData\Roaming\jupyter\runtime\kernel-v31ea843d7aea50337184f3b64926b0cfadb2be6f3.json
    > cwd: ~\Desktop\XXX\XXX\notebooks
14:55:01.541 [info] Kernel successfully started
14:55:01.547 [info] Process Execution: ~\mamba\envs\sticks\python.exe c:\Users\~\.vscode\extensions\ms-toolsai.jupyter-2024.10.0-win32-x64\pythonFiles\printJupyterDataDir.py
14:55:04.522 [error] Disposing session as kernel process died ExitCode: 3221225477, Reason:

#2 Micromamba venv activated in cli first then VSCode launched with "code" - Many more env vars available as well as important ones for geospatial work like GDA_DATA and GDAL_DRIVER_PATH. Env vars and site packages related errors are still showing, but the issue is resolved and the data query works with no crashes.

envvars
GDAL_DATA=C:\Users\XXX\mamba\envs\sticks\Library\share\gdal
GDAL_DRIVER_PATH=C:\Users\XXX\mamba\envs\sticks\Library\lib\gdalplugins
MAMBA_EXE=C:\Users\XXX\.local\bin\mamba.exe
MAMBA_ROOT_PREFIX=C:\Users\XXX\mamba
CONDA_DEFAULT_ENV=sticks
CONDA_PREFIX=C:\Users\XXX\mamba\envs\sticks
CONDA_PROMPT_MODIFIER=(sticks) 
CONDA_SHLVL=1
__CONDA_OPENSSL_CERT_DIR_SET=1
__CONDA_OPENSSL_CERT_FILE_SET=1

jupyter extension log
Visual Studio Code (1.95.3, undefined, desktop)
Jupyter Extension Version: 2024.10.0.
Python Extension Version: 2024.20.0.
Pylance Extension Version: 2024.11.3.
Platform: win32 (x64).
Temp Storage folder ~\AppData\Roaming\Code\User\globalStorage\ms-toolsai.jupyter\version-2024.10.0
Workspace folder ~\Desktop\XXX\XXX, Home = c:\Users\XXX
14:20:20.034 [info] Starting Kernel (Python Path: ~\mamba\envs\sticks\python.exe, Conda, 3.12.7) for ~\Desktop\XXX\XXX\notebooks\check_etl.ipynb (disableUI=true)
14:20:20.177 [error] Failed to get activated conda env vars for ~\mamba\envs\sticks\python.exe
                 in 143ms
14:20:20.178 [error] Unable to determine site packages path for python ~\mamba\envs\sticks\python.exe (Conda)
14:20:20.182 [info] Process Execution: ~\mamba\envs\sticks\python.exe -m pip list
14:20:20.184 [error] Failed to get activated conda env vars for ~\mamba\envs\sticks\python.exe
                 in 137ms
14:20:20.185 [error] Unable to determine site packages path for python ~\mamba\envs\sticks\python.exe (Conda)
14:20:20.189 [info] Process Execution: ~\mamba\envs\sticks\python.exe -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
14:20:20.193 [info] Process Execution: ~\mamba\envs\sticks\python.exe c:\Users\~\.vscode\extensions\ms-toolsai.jupyter-2024.10.0-win32-x64\pythonFiles\vscode_datascience_helpers\kernel_interrupt_daemon.py --ppid 7176
    > cwd: ~\.vscode\extensions\ms-toolsai.jupyter-2024.10.0-win32-x64\pythonFiles\vscode_datascience_helpers
14:20:20.287 [info] Process Execution: ~\mamba\envs\sticks\python.exe -m ipykernel_launcher --f=c:\Users\~\AppData\Roaming\jupyter\runtime\kernel-v37ef62efeacb8a2cb6d1542d0881d6886eee8d6f3.json
    > cwd: ~\Desktop\XXX\XXX\notebooks
14:20:21.234 [info] Kernel successfully started
14:20:21.239 [info] Process Execution: ~\mamba\envs\sticks\python.exe c:\Users\~\.vscode\extensions\ms-toolsai.jupyter-2024.10.0-win32-x64\pythonFiles\printJupyterDataDir.py

#3 Micromamba env not activated beforehand - but now miniforge3 is also installed on the same pc. - There seems to be a mix of miniforge and mamba env vars. Env vars and site packages related errors are gone for some reason.

envvars
CONDA_DEFAULT_ENV=sticks
CONDA_EXE=C:\Users\XXX\miniforge3\Scripts\conda.exe
CONDA_PREFIX=C:\Users\XXX\mamba\envs\sticks
CONDA_PROMPT_MODIFIER=(sticks) 
CONDA_PYTHON_EXE=C:\Users\XXX\miniforge3\python.exe
CONDA_ROOT=C:\Users\XXX\miniforge3
CONDA_SHLVL=1
_CONDA_OLD_CHCP=437
__CONDA_OPENSSL_CERT_DIR_SET="1"
__CONDA_OPENSSL_CERT_FILE_SET="1"
GDAL_DATA=C:\Users\XXX\mamba\envs\sticks\Library\share\gdal
GDAL_DRIVER_PATH=C:\Users\XXX\mamba\envs\sticks\Library\lib\gdalplugins

jupyter extension log
Visual Studio Code (1.95.3, undefined, desktop)
Jupyter Extension Version: 2024.10.0.
Python Extension Version: 2024.20.0.
Pylance Extension Version: 2024.11.3.
Platform: win32 (x64).
Temp Storage folder ~\AppData\Roaming\Code\User\globalStorage\ms-toolsai.jupyter\version-2024.10.0
Workspace folder ~\Desktop\XXX\XXX, Home = c:\Users\XXX
14:08:29.507 [info] Starting Kernel (Python Path: ~\mamba\envs\sticks\python.exe, Conda, 3.12.7) for ~\Desktop\XXX\XXX\notebooks\check_etl.ipynb (disableUI=true)
14:08:31.286 [info] Process Execution: ~\mamba\envs\sticks\python.exe -m pip list
14:08:31.291 [info] Process Execution: ~\mamba\envs\sticks\python.exe -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
14:08:31.294 [info] Process Execution: ~\mamba\envs\sticks\python.exe c:\Users\~\.vscode\extensions\ms-toolsai.jupyter-2024.10.0-win32-x64\pythonFiles\vscode_datascience_helpers\kernel_interrupt_daemon.py --ppid 12108
    > cwd: ~\.vscode\extensions\ms-toolsai.jupyter-2024.10.0-win32-x64\pythonFiles\vscode_datascience_helpers
14:08:31.375 [info] Process Execution: ~\mamba\envs\sticks\python.exe -m ipykernel_launcher --f=c:\Users\~\AppData\Roaming\jupyter\runtime\kernel-v3ae244917e03ac55c8ec7f793925650225c06835f.json
    > cwd: ~\Desktop\XXX\XXX\notebooks
14:08:32.324 [info] Kernel successfully started
14:08:32.330 [info] Process Execution: ~\mamba\envs\sticks\python.exe c:\Users\~\.vscode\extensions\ms-toolsai.jupyter-2024.10.0-win32-x64\pythonFiles\printJupyterDataDir.py
DonJayamanne commented 2 days ago

14:20:20.184 [error] Failed to get activated conda env vars for ~\mamba\envs\sticks\python.exe                  in 137ms

We're failing to get activated env variables for Python env.

karthiknadig commented 1 day ago

@AmerM137 does this work for you with python extension version 2024.16.1 or 2024.14.1 ?

AmerM137 commented 1 day ago

@karthiknadig I downgraded to the versions you mentioned. I see the same issue, no change. Env vars are not being loaded and kernel crashes immediately on DB query.