Jupyter 服务器的配置

Jupyter 服务器的配置

(一)Windows 系统

前提条件:

  • 已经安装好 python

打开 cmd, 输入以下指令

python 和 pip 版本检查:

1
2
python --version
pip --version

安装 Jupyter

1
pip install jupyter

检查 jupyter 是否安装成功

1
pip show jupyter

启动jupyter

1
python -m jupyter notebook

在输出中会包含地址,形如:

1
http://127.0.0.1:8888/tree?token=21ef9c83e4463384xxxxxxxxxxxxxxxxx

按住 ctrl 并点击这个地址即可在浏览器中打开

至此本地 jupyter 已经安装完毕,接下来开启局域网 Jupyter 服务

生成配置文件

1
python -m notebook --generate-config

这条命令会生成配置文件,地址在 C:\Users[Username].jupyter\jupyter_notebook_config.py
打开文件,加上以下内容:

1
2
3
4
c.NotebookApp.ip = '0.0.0.0'  # 允许局域网访问
c.NotebookApp.port = 4399 # 设置端口(可选)
c.NotebookApp.open_browser = False # 禁止自动打开浏览器
c.NotebookApp.browser = '' # 空字符串表示不使用任何浏览器

再次启动 jupyter

1
python -m jupyter notebook

此时输出中会出现新的 URL,形如:
http://[Username]:4399/tree?token=685ea3138adfe8edd31f8b51xxxxxxxxxxxxx
这就意味着当前的 jupyter 服务已经在 4399 端口上打开,局域网内都可以访问

记得去打开 Windows 防火墙的 4399 端口!

查看内网 IP 地址:

1
ipconfig

找到比如 10.107.54.247(校园网内网)或者如 192.168.0.44(家用路由器内网)

在局域网内的另一台设备上,打开浏览器, 输入URL:10.107.54.247:4399 就可以访问Jupyter 服务器了

在校园网内存在 AP 隔离,也就是两台连上校园网的电脑之间是不可以互相通信的,因此无法访问
但是 AP 隔离很好破解,详见 我的另一篇文章

接下来是一些优化

1. 设置密码

查看安装路径:

1
2
where python
pip show jupyter

一般显示的路径如:
C:\Users\[Username]\AppData\Local\Programs\Python\Python312\Lib\site-packages
我们需要把 python的Scripts 目录添加到环境变量中,如:
C:\Users\[Username]\AppData\Local\Programs\Python\Python312\Scripts

把安装路径添加到环境变量:
右键点击“此电脑” -> 选择“属性” -> 点击“高级系统设置” -> 点击“环境变量”。
在“系统变量”区域,找到 Path 变量,点击“编辑”。
点击“新建”,然后将以下路径添加进去:
C:\Users\[Username]\AppData\Local\Programs\Python\Python312\Scripts
C:\Users\[Username]\AppData\Local\Programs\Python\Python312\
保存设置后,关闭并重新打开命令行窗口,以确保新的环境变量生效。

在 cmd 中打开 python解释器

1
python

输入:

1
2
from jupyter_server.auth import passwd
passwd()

这会提示你输入一个密码,然后返回一个类似于以下格式的加密哈希值:
‘argon2:$argon2id$v=19$m=10240,t=10,p=8$gWbxxxxxxbO3/xxx/xxx/g$NhCKzzxxxxUyxtSY2hKn/m8Rxxxx’

生成密码哈希后,回到配置文件 jupyter_notebook_config.py, 加上

1
c.NotebookApp.password = u'argon2:$argo...'  # 设置密码(替换为你生成的密码哈希)

重启 服务器

1
python -m notebook

再尝试访问网页:10.107.54.247:4399
现在就会要求输入密码了

2. 指定路径

例如,如果你希望将 Jupyter Notebook 的根目录设置为 D:\Notebooks,
则在 配置文件jupyter_notebook_config.py中加上

1
c.NotebookApp.notebook_dir = r'D:\Notebooks'

重启 服务器

1
python -m notebook --no-browser

此时访问 Jupyter 时,它将默认打开你指定的目录(D:\Notebooks)作为根目录。

3. 开机自启动

新建文件 start_jupyter.bat,用记事本打开
输入内容如下:

1
2
3
@echo off
cd C:\Users\HeiheServer\AppData\Local\Programs\Python\Python312\Scripts
start /B python -m notebook --no-browser --port=4399

这个文件随便保存在哪里,比如放在桌面即可,但是要记一下路径

新建文件 start_jupyter.vbs,用记事本打开
输入内容如下:

1
2
3
Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run "your\path\to\start_jupyter.bat", 0
Set WshShell = Nothing

按住 win+R , 输入 shell:startup,回车进入启动脚本文件夹
将 start_jupyter.vbs 复制到该文件夹,双击即可运行。

现在,重启计算机后 Jupyter Notebook 服务器会自动启动。 CMD 窗口不会弹出,Jupyter 会在后台运行。


Jupyter 服务器的配置
http://www.heihet09.com/blog/2025/03/01/部署 Jupyter 服务器/
作者
heihe
发布于
2025年3月1日
许可协议