美文网首页
树莓派安装jupyter notebook,设置自启动

树莓派安装jupyter notebook,设置自启动

作者: 萧酃 | 来源:发表于2021-03-09 14:17 被阅读0次

预安装

1> 树莓派原生OS不带pip工具,安装pip管理工具,原生OS上有两个python版本,需指定版本(python3-pip/python2-pip)

sudo apgget install python3-pip

2>切换root用户
notes:直接切换会报没权限错误,需先设置root密码,执行下列命令,然后设置密码;

sudo passwd root

执行“su 用户名”命令切换用户,后面执行所有的命令都是在Root管理者权限下执行,(在pi下不能实现自启动)

su root

安装配置jupyter

1 安装jupyter,国内镜像源下载会更块,-i参数指定 https://pypi.douban.com/simple/, 其中pyzmq-22.0.3-cp37-cp37m-linux_armv7l.whl包下载容易报错,建议电脑本地安装

pip3 install -i https://pypi.douban.com/simple/ jupyter

2配置jupyter
安装完成后,执行下列命令

  jupyter notebook --generate-config

此时在/root/下会生成一个/.jupyter文件夹

root@raspberrypi:~# ls -al
total 56
drwx------  6 root root  4096 Mar  8 07:17 .
drwxr-xr-x 18 root root  4096 Jan 11 05:15 ..
-rw-------  1 root root  3288 Mar  8 07:18 .bash_history
-rw-r--r--  1 root root   570 Dec  9 14:40 .bashrc
drwx------  3 root root  4096 Mar  8 06:14 .cache
drwx------  2 root root  4096 Mar  8 07:14 .jupyter
drwxr-xr-x  3 root root  4096 Mar  8 06:22 .local
-rw-r--r--  1 root root   148 Dec  9 14:40 .profile
-rw-------  1 root root    49 Mar  8 06:29 .python_history
-rw-------  1 root root 13803 Mar  8 07:17 .viminfo
drwx------  3 root root  4096 Mar  8 01:20 .vnc
root@raspberrypi:~# cd .jupyter/
root@raspberrypi:~/.jupyter# ls -al

终端输入,

jupyter notebook password

提示输入密码,此处会生成密钥保存json文件中,此处后面会用到

root@raspberrypi:~/.jupyter# ls -al
total 60
drwx------ 2 root root  4096 Mar  8 07:14 .
drwx------ 6 root root  4096 Mar  8 07:17 ..
-rw------- 1 root root   129 Mar  8 06:50 jupyter_notebook_config.json
-rw-r--r-- 1 root root 48533 Mar  8 07:14 jupyter_notebook_config.py

打开/.jupyter 文件后编辑“jupyter_notebook_config.py”

c.NotebookApp.ip='*'                                              //‘*’表示局域网内任意IP都可以访问,
c.NotebookApp.allow_remote_access=True         //表示接受远程连接
c.NotebookApp.password=u'argon2:$argon2id$v=19$m=10240,t=10,p=8$QImcaSHkIZuOzmZwNgduTQ$ZSHvDSq4OZNX8ZonXgnbuA'      //保存在Json文件中的密钥
c.NotebookApp.open_browser=False                   //默认不打开浏览器
c.NotebookApp.port=48888                                  //默认端口
c.NotebookApp.notebook_dir='/home/pi/'             //默认工作目录

设置自启动

此处有多种方法,
方法1>编辑/etc.rc.local 文件,在exit 0 前加入 “jupyter notebook --allow-root”

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
jupyter notebook --allow-root
exit 0

方法二
在/etc/init.d/文件夹下增加执行文件jupyter.init

#!/bin/bash
case "$1" in
     start)
         echo "starting jupyter notebook"
         jupyter notebook --allow-root &
         ;;
     stop)
        echo "stopping jupyter notebook"
        kill $(ps aux|grep -m 1 'jupyter-notebook --allow-root'|awk '{ print $2}')
        ;;
     *)
        echo "Usage: service jupyter notebook start|stop"
        exit 1
        ;;
esac
exit 0

添加执行权限

chmod +x /etc/init.d/jupyter.init

重启设备

#运行jupyter.init 文件
sudo service jupyter.init start
#终止jupyter.init 文件
sudo service jupyter.init stop

设置开机启动

sudo update-rc.d jupyter.init defaults

设置好后重启设备

相关文章

网友评论

      本文标题:树莓派安装jupyter notebook,设置自启动

      本文链接:https://www.haomeiwen.com/subject/sujvqltx.html