Jupyter Lab是什么?我用多说,网上百度一大把。

我的树莓派os是官网下载的2021-10-30的版本,刷完系统自带python 3.9.2。

1. 安装

使用Jupyter官网的教程,直接安装Jupyter Lab:

$ pip3 install jupyterlab

居然出错了:

ERROR: Command errored out with exit status 1:
command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-tv6azl3y/cffi_79359e18e89b40a5b626890ecf238c19/setup.py'"'"'; __file__='"'"'/tmp/pip-install-tv6azl3y/cffi_79359e18e89b40a5b626890ecf238c19/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-mowal4g9
cwd: /tmp/pip-install-tv6azl3y/cffi_79359e18e89b40a5b626890ecf238c19/
Complete output (56 lines):
Package libffi was not found in the pkg-config search path.
Perhaps you should add the directory containing `libffi.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libffi' found
Package libffi was not found in the pkg-config search path.
Perhaps you should add the directory containing `libffi.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libffi' found
Package libffi was not found in the pkg-config search path.
Perhaps you should add the directory containing `libffi.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libffi' found
Package libffi was not found in the pkg-config search path.
Perhaps you should add the directory containing `libffi.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libffi' found
Package libffi was not found in the pkg-config search path.
Perhaps you should add the directory containing `libffi.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libffi' found

一堆英文,欲哭无泪,其实重点在第5行,后面不断重复提醒,我就是没留意(T T)。

发现问题,装上就是了:

$ sudo apt-get install libffi-dev

装上libffi-dev包,再重新安装Jupyter Lab,马上OK。

2. 配置

创建配置文件,以下2.1和2.2选自己安装的来执行就可以了

2.1 Jupyter Notebook的配置

在终端下执行命令:

$ jupyter notebook --generate-config
Writing default config to: /home/pi/.jupyter/jupyter_notebook_config.py

修改设置局域网能访问Jupyter lab

编辑刚刚生成的配置文件

$ vi ~/.jupyter/jupyter_notebook_config.py

在文件最后面添加一下配置:

# 配置Jupyter Lab默认目录,此目录要在Jupyter Lab启动前先创建
c.NotebookApp.notebook_dir = '/home/pi/jupyternotebook'
# 启动时不自动打开浏览器
c.NotebookApp.open_browser = False
# 配置其他终端可访问
c.NotebookApp.ip = '0.0.0.0'
# 配置访问端口
c.NotebookApp.port = 8888

我使用的版本的配置文件找不到“c.NotebookApp”的项目,所以只有自己加上去了。

2.2 Jupyter Lab的配置

在终端下执行命令:

$ jupyter lab --generate-config
Writing default config to: /home/pi/.jupyter/jupyter_lab_config.py

修改设置局域网能访问Jupyter lab

编辑刚刚生成的配置文件

$ vi ~/.jupyter/jupyter_lab_config.py

在文件最后面添加一下配置:

# 配置jupyterlab允许远程访问
c.ServerApp.allow_remote_access = True
# 默认启动时不打开浏览器
c.ExtensionApp.open_browser = False
# 允许任何IP访问
c.ServerApp.ip = '0.0.0.0'
c.ServerApp.password_required = False
# 配置访问端口
c.ServerApp.port = 8888
c.ServerApp.token = ''
# 配置Jupyter Lab默认目录,此目录要在Jupyter Lab启动前创建
c.ServerApp.notebook_dir = '/home/jetson/jupyterlab'

命令行方式配置

如果不用上面的配置文件,也可以使用命令行加参数的方式指定

$ jupyter lab --port='8088' --ip='*' --notebook-dir='/home/pi/jupyterlab' --no-browser

添加密码

访问Jupyter Lab,要不需要密码,要不需要token,还是添加一个密码比较方便

$ jupyter lab password


3. 启动Jupyter Lab

# 前端运行
$ jupyter lab
# 后台运行
$ jupyter lab &

4. 设置自动启动Jupyter Lab

在/etc/systemd/system增加服务

查找jupyter安装位置

$ which jupyter-lab
/home/pi/.local/bin/jupyter-lab

找到jupyter的安装路径,这里pi是我的用户名

创建jupyter.service文件

sudo vi /etc/systemd/system/jupyter.service

[Unit]

Description=Jupyter Lab


[Service]

Type=simple

User=pi

ExecStart=/home/pi/.local/bin/jupyter-lab --port 8888

WorkingDirectory=/home/pi/jupyterlab


[Install]

WantedBy=default.target

启动服务

$ sudo systemctl enable jupyter
$ sudo systemctl start jupyter

检查服务启动是否正常

运行以下命令检查服务状态,如果如下图输出,证明服务正常,重启后也能正常启动。

$ sudo systemctl status jupyter

Raspberry pi 树莓派安装 Jupyter lab_python