Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

virtualenv - Jupyter notebooks in Visual Studio Code does not use the active virtual environment

I write Python code in Visual Studio Code and run the program from a terminal in which I have activated a virtual environment, and it works fine.

However, if I create notebook cells using #%% and run those interactively, the virtual environment is not used. How can I fix this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's because there is an extra step needed - you need to explicitly install a Jupyter kernel that points to your new Python virtual environment. You can't simply activate Jupyter-lab or Notebook from the virtual environment. This has tripped me up before, too.

Follow the advice here: Using Jupyter notebooks with a virtual environment

And, in fact, there can be an issue where your kernel still doesn't point to the correct Python binary, in which case you need to change one suggestion in the above advice process:

From: ipython kernel install --user --name=projectname

To: python3 -m ipykernel install --user --name=projectname

(This correction comes from a comment to Jupyter Notebook is loading incorrect Python kernel #2563.)

*and don't forget to restart VSCode


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...