Jupyter
Python3 环境需提前准备好
如有需要,可以看这一篇《CentOS7 编译安装Python3.9.6》
不得不说,安装过程坑可真不少…希望能给各位带来些帮助,少走些弯路
-
安装
pip3 install jupyter
安装完成后,输入启动jupyter notebook试试
jupyter notebook
提示jupyter命令不存在
那么先查看jupyter安装的位置
find / -name jupyter
/usr/local/python3/etc/jupyter /usr/local/python3/share/jupyter /usr/local/python3/bin/jupyter
其中bin是存放命令的,所以把它添加到环境变量
vim /etc/profile # 在文件中添加下面这行 export PATH=$PATH:/usr/local/python3/bin/ # 保存退出 :wq # 更新配置 source /etc/profile
然后jupyter命令就可以正常使用了
-
配置jupyter
# 生成配置文件 jupyter notebook --generate-config --allow-root
(该文件路径默认为: ~/.jupyter/jupyter_notebook_config.py)
进入python3环境:
from notebook.auth import passwd passwd() # 随后会要求2次输入密码,该密码用于jupyter客户端登录 # 输入后会生成一个密钥(自行保存一下,稍后在jupyter配置文件中要用) exit()
python环境中的完整操作过程如下:
[root@python ~]# python3 Python 3.9.6 (default, Aug 3 2021, 14:59:02) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from notebook.auth import passwd >>> passwd() Enter password: ******** Verify password: ******** 'argon2:$argon2id$v=19$m=10240,t=10,p=8$28jw9sbYGlMey75R1198xw$/PbuQsPktKk/7ctO37BQIA' >>> exit()
编辑配置文件
vim ~/.jupyter/jupyter_notebook_config.py
在文件中第1行直接复制粘贴下面内容:(当然,也可以在文件中找到对应的配置项取消注释后修改,那样确实更规范些,但是这里为了方便后续修改配置,直接在第1行加了)
c.NotebookApp.ip='*' # 就是设置所有ip皆可访问 c.NotebookApp.password ='argon2:...' # 刚才复制的那个密文 c.NotebookApp.open_browser = False # 禁止自动打开浏览器 c.NotebookApp.port =8888 # 随便指定一个端口 c.NotebookApp.notebook_dir = '/home/xxx' # 配置笔记存放的默认目录
保存退出
启动jupyter notebook
jupyter notebook --allow-root # root用户记得加 --allow-root
[root@python ~]# jupyter notebook --ip 0.0.0.0 --no-browser --allow-root [I 16:19:46.118 NotebookApp] Serving notebooks from local directory: /root [I 16:19:46.118 NotebookApp] Jupyter Notebook 6.4.0 is running at: [I 16:19:46.118 NotebookApp] http://python:8888/ [I 16:19:46.118 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [I 16:19:59.147 NotebookApp] 302 GET / (60.179.177.253) 0.640000ms [I 16:19:59.175 NotebookApp] 302 GET /tree? (60.179.177.253) 1.030000ms [I 16:20:10.560 NotebookApp] 302 POST /login?next=%2Ftree%3F (60.179.177.253) 125.340000ms
然后我们就可以在自己的设备上浏览器地址栏输入服务器ip:8888 就可以访问了
但这时候如果关闭了终端,那么jupyter就停了,所以可以用下面这条命令后台启动它
nohup jupyter notebook --ip 0.0.0.0 --allow-root > jupyter.log 2>&1 &