astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
19.98k stars 592 forks source link

uv .venv and VS Code Jupyter Notebook: Connecting to Kernel #6672

Open wesstone12 opened 2 weeks ago

wesstone12 commented 2 weeks ago

Hello and thanks for all the great work with uv!

I was hoping to be able to use VS Code Jupyter notebooks with just uv to install Python. Loading the .venv works fine, but when running a code block, it is stuck in an infinite loop trying to connect to a Kernel. This only happens with VS Code Jupyter Notebooks as well. If I have the web version installed but specify to use the python installed with uv venv --python 3.10 it works fine. Would be great to not have to have a web version installed and only use uv to manage Python but understand if there's no workaround. Thanks for the help!

PS C:\Users\USER\testing stuff> uv python list
cpython-3.12.5-windows-x86_64-none     <download available>
cpython-3.11.9-windows-x86_64-none     <download available>
cpython-3.10.14-windows-x86_64-none    C:\Users\USER\AppData\Roaming\uv\data\python\cpython-3.10.14-windows-x86_64-none\python.exe
cpython-3.9.19-windows-x86_64-none     <download available>
cpython-3.8.19-windows-x86_64-none     <download available>
pypy-3.7.13-windows-x86_64-none        <download available>
PS C:\Users\wesst\testing stuff> 
 *  History restored 

Jupyter installed as well

PS C:\Users\wesst\testing stuff> uv pip tree
fqdn v1.5.1
isoduration v20.11.0
└── arrow v1.3.0
    ├── python-dateutil v2.9.0.post0
    │   └── six v1.16.0
    └── types-python-dateutil v2.9.0.20240821
jsonpointer v3.0.0
jupyter v1.0.0
├── notebook v7.2.1
.....
Visual Studio Code (1.92.2, undefined, desktop)
Jupyter Extension Version: 2024.7.0.
Python Extension Version: 2024.12.3.
Pylance Extension Version: 2024.8.2.
Platform: win32 (x64).
Workspace folder ~\testing stuff, Home = c:\Users\USER
22:17:38.461 [info] Starting Kernel (Python Path: ~\testing stuff\.venv\Scripts\python.exe, Venv, 3.10.14) for '~\testing stuff\test.ipynb' (disableUI=true)
22:17:39.048 [info] Process Execution: ~\testing stuff\.venv\Scripts\python.exe -m pip list
22:17:39.060 [info] Process Execution: ~\testing stuff\.venv\Scripts\python.exe -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
22:17:39.268 [info] Process Execution: ~\testing stuff\.venv\Scripts\python.exe -c "import pip;print('6af208d0-cb9c-427f-b937-ff563e17efdf')"
22:17:39.410 [warn] No interpreter with path ~\example\.venv\Scripts\python.exe found in Python API, will convert Uri path to string as Id ~\example\.venv\Scripts\python.exe
22:17:39.458 [warn] Failed to get activated env vars for ~\.virtualenvs\Documents-f1-lh8zo\Scripts\python.exe in 208ms
22:17:39.460 [error] Unable to determine site packages path for python ~\.virtualenvs\Documents-f1-lh8zo\Scripts\python.exe (Venv)
22:17:39.471 [info] Process Execution: ~\.virtualenvs\Documents-f1-lh8zo\Scripts\python.exe c:\Users\~\.vscode\extensions\ms-toolsai.jupyter-2024.7.0-win32-x64\pythonFiles\vscode_datascience_helpers\kernel_interrupt_daemon.py --ppid 14064
    > cwd: ~\.vscode\extensions\ms-toolsai.jupyter-2024.7.0-win32-x64\pythonFiles\vscode_datascience_helpers
22:17:41.868 [warn] Disposing old controller startUsingPythonInterpreter:'.jvsc74a57bd0425368e222807f297773663fdefa9d8713b24c56578a546e4a90a8565a35668f.c:\Users\~\AppData\Local\Programs\Python\Python313\python.exe.c:\Users\~\AppData\Local\Programs\Python\Python313\python.exe.-m#ipykernel_launcher' for view = 'jupyter-notebook'
22:17:41.869 [warn] Disposing old controller startUsingPythonInterpreter:'.jvsc74a57bd0425368e222807f297773663fdefa9d8713b24c56578a546e4a90a8565a35668f.c:\Users\~\AppData\Local\Programs\Python\Python313\python.exe.c:\Users\~\AppData\Local\Programs\Python\Python313\python.exe.-m#ipykernel_launcher (Interactive)' for view = 'interactive'

Below seems to be main issue.

22:17:39.458 [warn] Failed to get activated env vars for ~\.virtualenvs\Documents-f1-lh8zo\Scripts\python.exe in 208ms
22:17:39.460 [error] Unable to determine site packages path for python ~\.virtualenvs\Documents-f1-lh8zo\Scripts\python.exe (Venv)
22:17:39.471 [info] Process Execution: ~\.virtualenvs\Documents-f1-lh8zo\Scripts\python.exe c:\Users\~\.vscode\extensions\

Python scripts run fine

PS C:\Users\USER\testing stuff> uv run .\test.py   
hello uv
zanieb commented 2 weeks ago

Very weird, thanks for the report and sorry you ran into troubles here. We might need to escalate this to someone that's familiar with the Jupyter extension (cc @dhruvmanila), I have no idea why it would fail like that.

dhruvmanila commented 2 weeks ago
22:17:39.268 [info] Process Execution: ~\testing stuff\.venv\Scripts\python.exe -c "import pip;print('6af208d0-cb9c-427f-b937-ff563e17efdf')"
22:17:39.410 [warn] No interpreter with path ~\example\.venv\Scripts\python.exe found in Python API, will convert Uri path to string as Id ~\example\.venv\Scripts\python.exe

Huh, why is it looking for the interpreter in the "example" directory when the environment should be in "testing stuff" directory? How did you select the kernel in VS Code?

dhruvmanila commented 2 weeks ago

Which kernel do you see here? Can you click on it and select the current virtual environment?

Screenshot 2024-08-28 at 12 14 04
wesstone12 commented 2 weeks ago

Thanks for following up and yep, just the typical set up with the kernel. Actually just solved it by deleting below folder in Users\user Seems I hadn't cleaned out thoroughly imageThanks again!

dhruvmanila commented 2 weeks ago

Yeah, I guess there must be some mismatch between the (old) kernel configuration and the (new) virtual environment itself.