0

I use Jupyter extension (v2024.10.0), Python (v2024.20.0), and Pylance (v2024.12.1) with default interpreter path, VS Code 1.95.3 on MacOS Sonoma 14.6. I want to debug Python code interactively in jupyter notebooks using the same virtual environment I use when running modules via Terminal.

e.g. I have a project with these files:

  • test.ipynb
  • script.py

I run my script like this:

$ source ~/envs/my-project-env/bin/activate
(my-project-env) $ python script.py

I would like to use my-project-env environment in the Jupyter notebook. I click Select Kernel top right, but it doesn't appear in the drop-down.

enter image description here

Next I try the tip here and use ipython kernel install --user --name=my-project-env. It creates the kernel, and I can select it from the Jupyter Kernel... list instead, but it uses the wrong Python path, pointing to my global default interpreter instead.

Next I try this tip, added my python interpreter by adding my executable's path to the list of Python Interpreters. This works great for creating a dedicated terminal for running the script.py. No need for manually sourcing my environment anymore:

enter image description here

But the kernel is still unavailable for selection in Jupyter Notebook "Select Kernel" list. How can I run the Jupyter Notebook and get the same code auto-completion using the same environment as my script?

1
  • did you activate the environment before installing ipykernel and doing ipython kernel install --user --name=my-project-env? I bet you didn't and you did it using the default environment Commented Dec 9, 2024 at 21:47

2 Answers 2

2

Add the folder in which you stored the "my-project-env" to the VSCode workspace.

Sign up to request clarification or add additional context in comments.

1 Comment

Just adding additional step. How to add a folder into VS Code workspace? - From File > Add Folder to Workspace > Select appropriate folder Putting this comment as I had to search it separately
0

The only way I could figure this out was by creating a new Python environment via "Create Python Environment" option in Select Kernel menu:

enter image description here

This created a virtual environment called ".venv" in the root of my project folder, which is available for use with both my scripts and the Jupyter Kernel.

!which python
import sys
sys.executable

both display expected results, and packages can be imported no problem.

It's a decent solution, although I'd rather be able to re-use an existing environment in many cases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.