Set jupyter to start python2 or python3 as kernel

(Author: Xiaobaibaibaiyoubai cdllp, data-master.net )
Many times, although we are used to using a certain version of python, the version of python that may need to be used will change, for example, we use it in the machine environment of our partner python, or when you get another version of python code but don't have time to change the code.

Then the most convenient way is that we let python2 and python3 coexist, and we can choose which version to use.

1. Install python2 and python3 environment

The premise of achieving this goal is that we need to have python2 and python3 environments at the same time, so we need to install python2 and python3 at the same time, or install python2 version of anaconda and python3 version of anaconda at the same time. The premise of installing two versions of anaconda is Your machine storage is large enough.

2. Start two versions of jupyter

In the second step, you need to start two versions of jupyter. If anaconda is installed, then jupyter is already installed. If it is an installed python package, you also need to install jupyter yourself.

nohup /usr/local/anaconda3/bin/jupyter-notebook &
nohup /usr/local/anaconda2/bin/jupyter-notebook &

Start jupyter of python2 and python3 respectively (I seem to only start python2 here, but python2 is started by default in the system, so both 2 and 3 are available);

3. Abnormalities of startup

I encountered such a problem at the time, that is, I started the jupyter version of python3 in the background and
nohup /usr/local/anaconda3/bin/jupyter-notebook &
set it c.NotebookApp.open_browser = True(this property is in the jupyter configuration file, which means that jupyter can be opened in the browser), but python3 cannot be opened in the background jupyter.

So try to start jupyter directly on the front end, without adding nohup and &, and not starting in the background, so that it is easy to find errors.
insert image description here
Sure enough, an error was reported:
ModuleNotFoundError: No module named 'jupyter_nbextensions_configurator'

So, to check how to correct the error, it is actually a lack of package. Refer to this article: https://blog.csdn.net/lbj1260200629/article/details/103302287, execute python3.7 -m pip install jupyter_contrib_nbextensionsthe installation , restart jupyter, and start the front end without reporting an error. You can execute the command that started in the background in the previous step.

Also, just in case, this section in the config file is set:

## A class for managing multiple kernels.

## The name of the default kernel to start
c.MultiKernelManager.default_kernel_name = 'python3.6'

## The kernel manager class.  This is configurable to allow subclassing of the
#  KernelManager for customized behavior.
c.MultiKernelManager.kernel_manager_class = 'jupyter_client.ioloop.IOLoopKernelManager'

The comment I understand means that jupyter allows multiple kernels to exist, and sets the default kernel version to python3.6

Then it worked. After success, open jupyter as shown below:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_39750084/article/details/106876770