Jupyter and conda virtual environment link problem and related configuration

Problem Description:

Use the command:
conda create -n env_name python=ver

The created virtual environment can be seen with the command:
conda info --envs
, but after launching jupyter notebook, it cannot be found in the drop-down menu of new, and it can also be found in the kernel-change kernel menu of the editing interface after the file is opened. Can't find it.

 

Solution:

1. First install nb_conda_kernels in the kernel environment (real environment) where jupyter notebook is installed (if already installed, skip this step), use the command
conda install nb_conda_kernels
nb_conda_kernels to search for all ipython kernels under anaconda

2. For an existing virtual conda environment env_name, use
conda activate env_name to
switch to the virtual environment,
and confirm whether the ipykernel package is installed, if not, run it.
Note that the installed ipykernel cannot be higher than the ipykernel of root conda, otherwise an error may occur. If the ipykernel version of root conda is lower, it is recommended to install the corresponding lower version ipykernel in the virtual environment. It is not recommended to update the ipykernel of root conda separately, which may cause compatibility issues with the packages in root.
After switching to the virtual environment, execute:
conda install -n env_name ipykernel=version number
Install ipykernel, and then enter the command in the virtual environment:
python -m ipykernel install --user --name your_virtual_env_name --display-name "your_display_name" 

#ps: Running this command in the python3.8 virtual environment is ok, but it will report an error in the 2.7 virtual environment. If you don’t need conda install when installing ipykernel, you can change it to pip -m install. The way is also the latter. I don't know why the conda installation method produces this error

Complete the link configuration between the virtual environment kernel and jupyter.
View/delete the link configuration between jupyter and the kernel:

After completing the link configuration between the virtual environment and jupyter, you can perform the following view/delete operations:

View the link list of jupyter and virtual environment kernel and configuration storage location:
jupyter kernelspec list

Delete the link between jupyter and the kernel:
jupyter kernelspec remove your virtual environment kernel name
Note: If you want to further delete the virtual environment, you also need to run:
conda remove -n your virtual environment name --all

 

Reference link:
https://ipython.readthedocs.io/en/stable/install/kernel_install.html
https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernelspecs
https://stackoverflow.com /questions/39604271/conda-environments-not-showing-up-in-jupyter-notebook
https://stackoverflow.com/questions/42635310/remove-kernel-on-jupyter-notebook#:~:text=Also%2C 20you 20can% 20delete%%%% 20with% 20a% 20kernel 20jupyter, the Remove% 20one% 20or% 20more% 20Jupyter% 20kernelspecs% 20by% 20name.
https://blog.csdn.net/weixin_44135282/article/details/90704266

 

Guess you like

Origin blog.csdn.net/yocencyy/article/details/103403933