树莓派搭建jupyter notebook,python开发环境
一:环境准备
1.1:准备工作
1.1.1:安装pip3
执行apt install python3-pip
1.1.2:更换pip国内源(不然慢到你怀疑人生)
运行如下命令
mkdir ~/.pip
vim ~/.pip/pip.conf
pip.conf 配置如下
[global]
index-url = https://mirrors.aliyun.com/pypi/simple
当然你也可以用别的源
国内其他pip源
- 清华:https://pypi.tuna.tsinghua.edu.cn/simple
- 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
- 华中理工大学:http://pypi.hustunique.com/
- 山东理工大学:http://pypi.sdutlinux.org/
- 豆瓣:http://pypi.douban.com/simple/
更改其他源只需要更改配置文件中的index-url地址即可
2:插入到读卡器中,在把读卡器插入有Linux系统的电脑中,这里用虚拟机也可以
二:安装jupyter notebook
2.1:开始安装
执行pip3 install jupyter
安装成功后可以使用 pip3 show jupyter 查看安装位置
2.2:配置jupyter notebook
2.2.1:生成配置文件
执行jupyter notebook --generate-config
root@tao:~# jupyter notebook --generate-config
Writing default config to: /root/.jupyter/jupyter_notebook_config.py
2.2.2:编辑配置文件
执行vim ~/.jupyter/jupyter_notebook_config.py
搜索 c.NotebookApp.notebook_dir 可以看到上面的注释,这个配置为修改我们笔记的存放目录的,这里我们修改为我们自己建立的目录方便后期维护和管理
## 用于笔记本和内核的目录。
#c.NotebookApp.notebook_dir = ''
c.NotebookApp.notebook_dir = '/home/tao/notes'
## Whether to open in a browser after starting. The specific browser used is
# platform dependent and determined by the python standard library `webbrowser`
# module, unless it is overridden using the --browser (NotebookApp.browser)
# configuration option.
#c.NotebookApp.open_browser = True
## Hashed password to use for web authentication.
#
# To generate, type in a python/IPython shell:
#
:/c.NotebookApp.notebook_dir
注意这里的工作目录一定要有普通用户可写的权限,否则等会儿用自己的账户运行jupyter notebook 会出问题
sudo chown tao:tao notes/ #这里的tao是用户名
#也可以给一个777权限 这里不推荐
sudo chmod 775 -R notes/
2.2.3:创建jupyter notebook密码
执行jupyter notebook password
root@tao:~# jupyter notebook password
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json
2.2.4:启动服务
执行jupyter notebook
root@tao:~# jupyter notebook
[I 02:37:33.010 NotebookApp] 把notebook 服务cookie密码写入 /root/.local/share/jupyter/runtime/notebook_cookie_secret
[I 02:37:33.305 NotebookApp] JupyterLab extension loaded from /usr/local/lib/python3.7/dist-packages/jupyterlab
[I 02:37:33.305 NotebookApp] JupyterLab application directory is /usr/local/share/jupyter/lab
[C 02:37:33.307 NotebookApp] 不建议以root身份运行.使用--allow-root绕过过.
可以看到官方不建议我们使用root用户来启动这个服务,应为权限太大,不安全。
当然也可以强制启动
执行jupyter notebook --allow-root
这里我们使用普通用户来启动服务
注意这里切换用户后2.2:配置jupyter notebook 需要再来一遍 我这里省略了
root@tao:~# su - tao #tao 为你的用户名
tao@tao:~$ jupyter notebook
[I 02:44:41.714 NotebookApp] 把notebook 服务cookie密码写入 /home/tao/.local/share/jupyter/runtime/notebook_cookie_secret
[I 02:44:41.958 NotebookApp] JupyterLab extension loaded from /usr/local/lib/python3.7/dist-packages/jupyterlab
[I 02:44:41.958 NotebookApp] JupyterLab application directory is /usr/local/share/jupyter/lab
[I 02:44:41.960 NotebookApp] 启动notebooks 在本地路径: /home/tao
[I 02:44:41.960 NotebookApp] Jupyter Notebook 6.1.1 is running at:
[I 02:44:41.961 NotebookApp] http://localhost:8888/?token=8a61b3e1e524e36be032b4cad4f277b4a1f9d9cde9d8fdca
[I 02:44:41.961 NotebookApp] or http://127.0.0.1:8888/?token=8a61b3e1e524e36be032b4cad4f277b4a1f9d9cde9d8fdca
[I 02:44:41.961 NotebookApp] 使用control-c停止此服务器并关闭所有内核(两次跳过确认).
[W 02:44:41.964 NotebookApp] 没有找到web浏览器: could not locate runnable browser.
OK
最后浏览访问
http://127.0.0.1:8888/tree
或者你的IP地址都可以
三:开始我们的第一个python程序
打开浏览器输入地址
登录密码
点机右边的新建 ,让后选择pyhon3
在打开的界面
输入 print(“hello world”) 注意这里一定要自己输入不要粘贴我的否则会报错
点机运行就得到运行结果啦
最后可以保存你编辑的文件
点机工具栏最左面的哪个按钮即可保存
可以点机最上面的未命名可以修改文件名
最后如果这个帖子对你有帮助请给个赞
四:远程主机访问 notebook 的配置
4.1:修改配置文件
1:查看密钥文件,并复制密钥
cat /home/tao/.jupyter/jupyter_notebook_config.json
tao@tao:~/notes$ cat /home/tao/.jupyter/jupyter_notebook_config.json
{
"NotebookApp": {
"password": "argon2:$argon2id$v=19$m=10240,t=10,p=8$vVdKlqfAgmtBsd+/R+azuQ$uOFdv46cJbSBZLUj3KYd1Q"
}
2:把密钥复制到配置文件
编辑配置文件,修改一下四点内容
vim ~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.ip='*' #设置所有ip地址皆可访问 大概在206行
c.NotebookApp.notebook_dir = '/home/tao/notes' #设置notebook目录,不设置就在用户家目录下大概在268行
c.NotebookApp.password = 'argon2:$argon2id$v=19$m=10240,t=10,p=8$vVdKlqfAgmtBsd+/R+azuQ$uOFdv46cJbSBZLUj3KYd1Q' #刚才复制的那个密文,大概在283行
c.NotebookApp.port =8888 #可自行指定一个端口, 访问时使用该端口,大概在294行,这个根据自己情况修改,不改也不影响
c.NotebookApp.open_browser = False #如果没有安装图形桌面,把这行也改下,这个是默认启动打开浏览器并进入项目
3:浏览器IP+端口访问即可,输入密码
4.2:优化启动流程
由于启动后会停留在终端退出后就退出了软件
这里我们使用后台启动的方式,写入运行和启动日志到当前目录下
nohup jupyter notebook >jupyter.date '+%Y%m%d-%H%M%S'
.log 2>&1 &
tao@tao:~/桌面$ nohup jupyter notebook >test.`date '+%Y%m%d-%H%M%S'`.log 2>&1 &
[1] 7091
这里我写了个启动脚本
vim jupyter-lab.sh
#/bin/bash
datas=`date '+%Y%m%d-%H%M%S'`
#运行程序
nohup jupyter-lab >jupyter.$datas.log 2>&1 &
echo "jupyter-lab Start successfully"
chmod +X jupyter-lab.sh
一些常用命令
jupyter notebook --version #查看版本信息
conda install -c conda-forge jupyterlab # 这是用来升级 jupyter lab 到最新版的方法
jupyter notebook list # 查看正在运行的 jupyter lab/notebook
jupyter notebook stop # 停止 jupyter lab/notebook 服务
当然 jupyter notebook 启动可以加参数指定工作目录 例如
jupyter notebook /home/notes &