Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...