设置jupyter可启动python2或python3作为kernel

(作者:小白白白又白cdllp,data-master.net
很多时候,虽然我们习惯用某个版本的python,但可能需要使用的python版本会发生改变,比如我们在合作方的机器环境下使用python时,或者拿到一份其他版本的python代码但又没时间改代码时。

那么最方便的方法还是,我们让python2和python3并存,我们可以选择使用哪个版本。

1. 安装python2和python3环境

实现这个目标的前提,就是我们本机需要同时有python2和python3的环境,所以我们需要同时安装python2和python3,或者同时安装python2版本的anaconda和python3版本的anaconda,安装两个版本的anaconda的前提是你的机器存储够大。

2. 启动两个版本的jupyter

第二步就需要启动两个版本的jupyter,如果是安装的anaconda,那么jupyter就已经装好了的。如果是安装的python包,还需要自己额外安装一下jupyter。

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

分别启动python2和python3的jupyter(我这里好像只启动了python2,但系统里默认启动了python2的,所以2和3的都有);

3. 启动的异常情况

我当时碰到了这么个问题,就是执行了后台启动python3版本的jupyter,
nohup /usr/local/anaconda3/bin/jupyter-notebook &
也设置了c.NotebookApp.open_browser = True(这个属性是在jupyter配置文件中的,意思是可以在浏览器中打开jupyter),但是后台打不开python3的jupyter。

于是尝试直接在前端启动jupyter,不加nohup和&即可,不在后台启动,方便发现报错。
在这里插入图片描述
果然报错:
ModuleNotFoundError: No module named 'jupyter_nbextensions_configurator'

于是乎,查看怎么改掉错误,其实就是缺包,参考的这篇文章:https://blog.csdn.net/lbj1260200629/article/details/103302287,执行 python3.7 -m pip install jupyter_contrib_nbextensions安装,重新启动jupyter,前端启动不报错就可以再执行上一步在后台启动的那句命令了。

此外,为了以防万一,还设置了配置文件中的这个部分:

## 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'

我理解的注释的意思就是,jupyter允许多种内核存在,并且设置默认内核版本为python3.6

然后就成功啦。成功之后打开jupyter就如下图了:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39750084/article/details/106876770
今日推荐