关于虚拟环境的创建及管理见我另一篇文章pyenv管理python环境
安装jupyter notebook
我的系统版本:Centos7.9
在所在虚拟环境内安装jupyter notebook,同时也会自动安装ipython
(lwj361) [python@lura ~/lwj/projects/cmdb]$pip install jupyter
安装成功提示:
修改jupyter notebook密码:
[python@lura ~/lwj/projects/cmdb]$jupyter notebook --help
[python@lura ~/lwj/projects/cmdb]$jupyter notebook password
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to /home/python/.jupyter/jupyter_notebook_config.json
设置允许访问IP并启动jupyter notebook
[python@lura ~/lwj/projects/cmdb]$jupyter notebook --ip=0.0.0.0
ifconfig #查看localhost所在IP
使用 IP:8888 访问,然后输入你刚设置的密码即可
localhost端创建文件,在notwbook浏览器端都可以看到
将当前环境内所有的包 导出到一个文件内:pip freeze
(lwj361) [python@lura ~/lwj/projects/cmdb]$pip freeze > /tmp/requirement.txt
(lwj361) [python@lura ~/lwj/projects/cmdb]$cat /tmp/requirement.txt
argon2-cffi==21.1.0
async-generator==1.10
attrs==21.2.0
backcall==0.2.0
bleach==4.1.0
cffi==1.15.0
decorator==5.1.0
defusedxml==0.7.1
entrypoints==0.3
importlib-metadata==4.8.2
ipykernel==5.5.6
ipython==7.16.2
ipython-genutils==0.2.0
ipywidgets==7.6.5
jedi==0.17.2
Jinja2==3.0.3
jsonschema==4.0.0
jupyter==1.0.0
jupyter-client==7.1.0
jupyter-console==6.4.0
jupyter-core==4.9.1
jupyterlab-pygments==0.1.2
jupyterlab-widgets==1.0.2
MarkupSafe==2.0.1
mistune==0.8.4
........
这个文件保存下来,以后如果需要将同样的环境部署在别的项目里,直接 pip -r requirements.txt读取文件就可以
pip install -r requirement.txt
问题记录:
使用jupyter notebook 登陆时出现问题:
ImportError: cannot import name 'NoReturn'
[W 08:38:45.288 NotebookApp] KernelRestarter: restart failed
[W 08:38:45.288 NotebookApp] Kernel 429d9d0e-e748-4b1e-89ac-e1880c59b9b1 died, remo ving from map.
[W 08:39:27.239 NotebookApp] Timeout waiting for kernel_info reply from 429d9d0e-e7 48-4b1e-89ac-e1880c59b9b1
[E 08:39:27.245 NotebookApp] Error opening stream: HTTP 404: Not Found (Kernel does not exist: 429d9d0e-e748-4b1e-89ac-e1880c59b9b1)
在网上找了各种教程后发现是prompt-toolkit版本的问题
我根据提示将版本改为3.0.15就没有问题了,之前是3.0.19
pip install --upgrade prompt-toolkit==3.0.15
#卸载原来的版本
Attempting uninstall: prompt-toolkit
Found existing installation: prompt-toolkit 3.0.19
Uninstalling prompt-toolkit-3.0.19:
Successfully uninstalled prompt-toolkit-3.0.19
#安装3.0.15
[python@lura ~/lwj/projects/cmdb]$pip install --upgrade prompt-toolkit==3.0.15
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting prompt-toolkit==3.0.15
Downloading https://mirrors.aliyun.com/pypi/packages/67/cc/5c44c8b3b3e9f9069eff81a9fc3 54f417423558d394b4a9852c20b5ff164/prompt_toolkit-3.0.15-py3-none-any.whl (360 kB)
|████████████████████████████████| 360 kB 1.4 MB/s
Requirement already satisfied: wcwidth in /home/python/.pyenv/versions/3.6.1/envs/lwj361 /lib/python3.6/site-packages (from prompt-toolkit==3.0.15) (0.2.5)
Installing collected packages: prompt-toolkit
Attempting uninstall: prompt-toolkit
Found existing installation: prompt-toolkit 3.0.0
Uninstalling prompt-toolkit-3.0.0:
Successfully uninstalled prompt-toolkit-3.0.0
Successfully installed prompt-toolkit-3.0.15
可以看到现在使用
jupyter notebook --ip=0.0.0.0
进行连接就没有问题了