安装jupyter-notebook

$ pip install jupyter

配置jupyter-notebook

配置可以同时使用python2和python3内核

$ ipython kernel install --user
$ python3 -m ipykernel install --user
$ pip2 install -U ipykernel
$ python2 -m ipykernel install --user
$ jupyter-notebook  //运行,会自动web界面,可以同时运行python2,python3,ctrl+c结束

生成配置文件

$ cd
$ jupyter notebook --generate-config
Writing default config to: /root/.jupyter/jupyter_notebook_config.py

生成密码

运行jupyter

In [1]: from notebook.auth import passwd
        passwd()

    Enter password: ········
    Verify password: ········

Out[1]: 'sha1:c3a52264ad87:f6a2c3503ee3370c67da1f723ae1e8e79477f5f7'

设置密码

将前面生成的一串密码替换到配置文件中

$ vim /root/.jupyter/jupyter_notebook_config.py
c.NotebookApp.password=u'sha1:c3a52264ad87:f6a2c3503ee3370c67da1f723ae1e8e79477f5f7'   //前面u表示转换成unicode字符,python2的时候需要带

其它设置

c.NotebookApp.ip = '*'  //访问ip限制
c.NotebookApp.notebook_dir = '/home/knmax/Desktop/Python/jupyter-project'  //工作目录,路径不能出现中文
c.NotebookApp.open_browser = False //不自动打开浏览器
c.NotebookApp.port = 88 //运行监听的端口

以服务方式运行

每次运行打开都是终端交互的界面,关闭会话终端也结束了jupyter,很不方便,这里做成以systemctl方式启动,适用于Debain、CentOS 7、Ubuntu

$ vim /lib/systemd/system/jupyter.service  //这个目录不同发行版可能也不同
[Unit]
Description=jupyter
After=network.target
[Service]
Tpye=forking
EnvironmentFile=/usr/local/bin/jupyter-notebook
ExecStart=/usr/local/bin/jupyter-notebook
ExecStop=/usr/bin/pkill jupyter-notebook
KillMode=process
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target

创建好之后再操作下就行了