jupyter notebook的安装与使用

jupyter notebook的安装与使用

使用jupyter notebook有两种方法:

  1. 使用vscode里的插件直接运行jupyter程序。
  2. 使用原生的基于浏览器网页的方式访问,需要在终端里开启jupyter的服务。

方法一: VSCODE中使用jupyter

在vscode中新建.ipynb后缀的文件,并写好python程序,会提示你安装jupyter插件,装好对应的插件后,选择python解释器内核,即可直接运行使用。这种方式还是比较简单的。

方法二:浏览器web方式访问jupyter

浏览器访问的话比较麻烦,需要在后台开启jupyter服务,并保持运行。以下提供了python虚拟环境安装jupyter方法。

  1. 创建虚拟环境

首先,为Python 3.8创建一个独立的虚拟环境。这样可以避免与其他版本的Python发生冲突。可以使用Python内置的venv模块创建虚拟环境:

复制代码
python3.8 -m venv jupyter38

这将在当前目录下创建一个名为jupyter38的虚拟环境。

  1. 激活虚拟环境

    source jupyter38/bin/activate

  2. 安装Jupyter Notebook

在虚拟环境中,使用pip安装Jupyter Notebook:

复制代码
python -m pip install --upgrade pip
pip install jupyter notebook
  1. 启动Jupyter Notebook

安装完成后,您可以运行以下命令来启动Jupyter Notebook:

复制代码
jupyter notebook --no-browser --allow-root --port 8888
  1. 退出

    停止Jupyter Notebook

    Ctrl+C

    退出虚拟环境

    deactivate

  2. jupyter配置文件的使用

因为我的代码是需要root权限的,所以需要在root账户下操作。使用以下命令创建jupyter配置文件

复制代码
jupyter notebook --generate-config

生成的配置文件位于:/root/.jupyter/jupyter_notebook_config.py

常用的配置如下:

复制代码
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.password='sha1:a2d51b48e469:8346b129984754f8460065449568539703c54187' #密码123456
c.NotebookApp.open_browser=False #不在服务器打开浏览器
c.NotebookApp.allow_remote_access=True 
c.NotebookApp.port=8888 #端口 这里虽然设置了8888但是如果端口冲突会给你换一个开
c.NotebookApp.notebook_dir = '/root' #jupyter的工作目录
  1. 登陆密码的更改

使用命令jupyter notebook password,会提示你输入密码,设置好后会生成一个json文件位于

复制代码
/root/.jupyter/jupyter_notebook_config.json

内容为

复制代码
{
  "NotebookApp": {
    "password": "sha1:a2d51b48e469:8346b129984754f8460065449568539703c54187"
  }
}

密码的哈希值拷贝出来替换到配置文件中c.NotebookApp.password字段即可。

相关推荐
Csvn17 小时前
`functools.lru_cache` —— 一行代码搞定缓存加速
后端·python
金銀銅鐵1 天前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup112 天前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi002 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵2 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf2 天前
Agent 流程编排
后端·python·agent
copyer_xyf2 天前
Agent RAG
后端·python·agent
copyer_xyf2 天前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf2 天前
Agent 记忆管理
后端·python·agent