Jupyter Notebook 安装及配置

Errors

第一个报错的库是 typing-extensions,为了避免 tensorflow 版本依赖出问题,首先尝试降级到 4.5.0,然而在配置 Jupyter Notebook 账号密码时报错。从 Jupyter 仓库检查版本后,决定从 7.3 降至 6.5.6。

因为之前的 Python 是手动编译的,这里又埋了一个坑。运行 jupyter notebook --generate-config,报错

1
2
ModuleNotFoundError: No module named '_sqlite3'
ModuleNotFoundError: No module named 'pysqlite2'

检查 dpkg -l 是安装了 sqlite3 的,问题出在构建 Python3 时缺少 libsqlite3-dev 依赖。安装 libsqlite3-dev 后重新编译安装 Python 3.10.5。

安装

安装及配置如下。

1
2
pip install notebook==6.5.6
jupyter notebook --generate-config

设置密码

1
2
3
4
5
6
7
ipython
...

In [2]: passwd()
Enter password: (jupyter)
Verify password: (jupyter)
Out[2]: 'argon...3uWz+Q'

root 旁路启动

1
jupyter notebook --allow-root  # port 8888

win !

切换 Kernel

问题描述

启动 Notebook 后检查 Python 版本为 3.11.2,并非所期望的 3.10.5

1
2
3
4
5
> pip list | grep ipykernel
ipykernel 6.29.5
> jupyter kernelspec list
Available kernels:
python3 /usr/local/share/jupyter/kernels/python3

排查。

1
2
3
!python --version
import sys
print(sys.version)

其输出为

1
2
Python 3.11.2
3.10.5 (main, Dec 9 2024, 20:37:19) [GCC 11.3.0]

不一致!

为什么呢

检查了 Python 安装,发现当前是通过 alias 指定 Python 版本为 3.10.5,而 alias 的作用是定义自定义命令,在 Jupyter Notebook 中并不生效。

已经通过 echo $SHELL 检查,Jupyter Notebook 的 bash 环境和本地一致,均为 /bin/bash,做一个小测试。

在 ~/.bashrc 中写入如下内容

1
2
alias test-cmd='echo This command is for testing.'
export TEST_ENV="This environment variable is for testing."

在 Jupyter Notebook 中的结果为:

1
2
3
4
!test-cmd
!echo $TEST_ENV
/bin/bash: line 1: test-cmd: command not found
This environment variable is for testing.

可以看到,环境变量是生效的,但 alias 并不能生效。实际上,alias 只在当前 shell 有效,即便均为 /bin/bash,也是两个不同的窗口。因此会导致 !python --version 与 Kernel 的实际 Python 版本不一致。

总结

没有总结。

参考

  1. Linux 服务器 安装Jupyter notebook 并开启远程访问 - 组学大讲堂问答社区
  2. Ubuntu安装jupyter,启动出现ModuleNotFoundError: No module named ‘pysqlite2‘ 错误 - 盼小辉丶 - 博客园
  3. python中切换jupyter notebook版本_mob64ca12dc88a3的技术博客_51CTO博客
  4. python - ModuleNotFoundError: No module named ‘jupyter_server.contents’ - Stack Overflow
  5. Releases · jupyter/notebook
  6. 创建以及更换 Jupyter Notebook 内核 Python 版本_jupyter kernel 创建已经有的python 版本-CSDN博客