Introduction:

This is a transcript of my previous blog post on CSDN. Record of Jupyter kernel replacement problems in Different Conda environments on Linux

Problem description

On the server, multiple Conda environments exist. When different environments are used, the Python interpreter in JUPyter cannot be switched properly. When you run the command to view the Python interpreter path, the Python path entered from CMD is inconsistent with that used by the Kernel in the Notebook.

For example, there are two environments, NLP and TORCH18. First, activate the NLP environment and run Jupyter Lab from the server command line

>> conda activate nlp
>> jupyter lab --no-browser
Copy the code

Then, connect to the Jupyter remotely using a browser to view the path to the Python interpreter

import sys
print( sys.executable)
Copy the code

The result is

/home/jxqi/anaconda3/envs/nlp/bin/python
Copy the code

As you can see, it is the Python interpreter used in the NLP environment.

Next, close the Jupyter, switch to the Torch18 environment and run the Jupyter Lab

>> conda activate torch18
>> jupyter lab --no-browser
Copy the code

Then, connect to the Jupyter remotely using a browser to view the path to the Python interpreter

import sys
print( sys.executable)
Copy the code

The result is

/home/jxqi/anaconda3/envs/nlp/bin/python
Copy the code

As you can see, it is also the Python interpreter used in the NLP environment.

Ideally, it should automatically switch to the interpreter in torch18. There is no automatic switch, which will cause the package we installed in Torch18 to fail to import properly. When you run Python on the command line, you can import it.

The solution

Method 1: Delete the configuration file of the JUPyter kernel

After encountering this problem, I found that there was no kernel configuration file in his jupyter configuration file when discussing with other students. And his Jupyter is normal can automatically switch the corresponding environment of the interpreter. Therefore, I tried to directly delete the kernel configuration file in my directory, and then restart the Jupyter Lab. The problem was solved.

The kernel configuration file path is as follows:

First run the command to find the python3 kernel configuration file path

>> ipython kernelspec list
Copy the code

According to

Available kernels:
  python3    /home/jxqi/.local/share/jupyter/kernels/python3
  python2    /home/jxqi/.local/share/jupyter/kernels/python2
Copy the code

Then go to this directory and just delete the python3 folder. Personal test is effective.

Method 2: Add the kernel in python Conda

This method mainly refer to: add python to jupyter – notebook conda environment of kernel | | correct practical bsde in this blog. First activate the Torch18 environment and install the IPykernel.

>> conda activate torch18 
>> conda install  ipykernel
Copy the code

The environment is then written to Jupyter

>> python -m ipykernel install --user --name torch18 --display-name "torch18"
Copy the code

Finally restart the Jupyter Lab, you can switch in the kernel of the menu bar. However, every time you need to manually switch, the feeling is more troublesome. Therefore, the first method is recommended, which is simple and effective.

reference

  1. Add python to jupyter – notebook conda environment of kernel | | bsde, right blog.csdn.net/sinat_28442…