Jupyter Notebook 常用命令(自用)

最近有点忘记了一些常见命令,这里就记录一下,懒得找了。

文章目录

    • 一、文件操作命令
      • [1. `%cd` 工作目录](#1. %cd 工作目录)
      • [2. `%pwd` 显示路径](#2. %pwd 显示路径)
      • [3. `!ls` 列出文件](#3. !ls 列出文件)
      • [4. `!cp` 复制文件](#4. !cp 复制文件)
      • [5. `!mv` 移动或重命名](#5. !mv 移动或重命名)
      • [6. `!rm` 删除](#6. !rm 删除)
    • 二、代码调试
      • [1. `%time` 时间](#1. %time 时间)
      • [2. `%timeit` 平均时长](#2. %timeit 平均时长)
      • [3. `%debug` 调试](#3. %debug 调试)
      • [4. `%run` 跑python](#4. %run 跑python)
      • [5. `%load` 加载](#5. %load 加载)
    • 三、魔术命令
      • [1. `%matplotlib` 显示图片](#1. %matplotlib 显示图片)
      • [2. `%pylab` 导入Numpy和Matpolotlib](#2. %pylab 导入Numpy和Matpolotlib)
      • [3. `%%writefile` 单元格内容写入文件](#3. %%writefile 单元格内容写入文件)
      • [4. `%history` 历史命令](#4. %history 历史命令)
      • [5. `%who` 列出变量名](#5. %who 列出变量名)
      • [6. `%whos` 列出变量名及详情](#6. %whos 列出变量名及详情)
      • [7. `%xmode` 异常显示](#7. %xmode 异常显示)
    • 四、带叹号的
      • [1. `!pip`](#1. !pip)
      • [2. `!conda`](#2. !conda)
      • [3. `!git`](#3. !git)
      • [4. `!wget`](#4. !wget)
      • [5. `!curl`](#5. !curl)
      • [6. `!mkdir` 创建目录](#6. !mkdir 创建目录)

一、文件操作命令

1. %cd 工作目录

用于切换当前工作目录。

python 复制代码
%cd /home/test/test1/src

或者用python的代码

bash 复制代码
import os
os.chdir('/home/test/test1/src') # 切换
print(os.path.abspath('.')) # 打印看看

2. %pwd 显示路径

显示当前工作目录的路径。例如:

python 复制代码
%pwd

输出:

复制代码
'/home/user/works'

3. !ls 列出文件

列出当前目录下的文件和文件夹。例如:

python 复制代码
!ls

4. !cp 复制文件

复制文件。例如:

python 复制代码
!cp source_file.txt destination_file.txt

5. !mv 移动或重命名

移动或重命名文件。例如:

python 复制代码
!mv old_name.txt new_name.txt

6. !rm 删除

删除文件。例如:

python 复制代码
!rm file_to_delete.txt

二、代码调试

1. %time 时间

用于测量单个代码块的执行时间。例如:

python 复制代码
%time sum(range(1000000))

输出:

复制代码
CPU times: user 0.02 s, sys: 0.00 s, total: 0.02 s
Wall time: 0.02 s

2. %timeit 平均时长

多次运行代码块,给出平均执行时间,适合比较不同实现的性能。例如:

python 复制代码
%timeit sum(range(1000000))

输出:

复制代码
10 loops, best of 3: 100 ms per loop

3. %debug 调试

进入调试模式,用于调试代码中的错误。例如:

python 复制代码
%debug

4. %run 跑python

运行一个 Python 脚本文件。例如:

python 复制代码
%run test.py
python 复制代码
!python test.py

5. %load 加载

加载一个文件的内容到当前单元格。例如:

python 复制代码
%load data_processing.py

三、魔术命令

1. %matplotlib 显示图片

用于在 Notebook 中显示 Matplotlib 图形。例如:

python 复制代码
%matplotlib inline

2. %pylab 导入Numpy和Matpolotlib

一次性导入 NumPy 和 Matplotlib,并使其可用。例如:

python 复制代码
%pylab inline

3. %%writefile 单元格内容写入文件

将单元格内容写入文件。例如:

python 复制代码
%%writefile hello_world.py
print("Hello, World!")

4. %history 历史命令

显示历史命令记录。例如:

python 复制代码
%history

5. %who 列出变量名

列出当前命名空间中的变量名。例如:

python 复制代码
%who

6. %whos 列出变量名及详情

列出当前命名空间中的变量名及其详细信息。例如:

python 复制代码
%whos

7. %xmode 异常显示

设置异常显示模式。例如:

python 复制代码
%xmode Verbose

四、带叹号的

1. !pip

在 Notebook 中安装 Python 包。例如:

python 复制代码
!pip install numpy
bash 复制代码
!pip install -r requirements.txt --user
bash 复制代码
!pip install -r xxx/requirements.txt -t xxx/external-libraries
  • -r requirements.txt:表示从 requirements.txt 文件中读取依赖列表。
  • -t xxx/external-libraries:表示将安装的库放到指定的目录 xxx/external-libraries 下。

2. !conda

在 Notebook 中使用 Conda 命令。例如:

python 复制代码
!conda list

3. !git

在 Notebook 中使用 Git 命令。例如:

python 复制代码
!git clone https://github.com/user/repo.git

4. !wget

下载文件。例如:

python 复制代码
!wget https://example.com/data.csv

5. !curl

发送 HTTP 请求。例如:

python 复制代码
!curl https://api.example.com/data

6. !mkdir 创建目录

bash 复制代码
!mkdir -p xxx/external-libraries
相关推荐
全栈练习生7 小时前
大模型推理全链路
python·ai
泡海椒8 小时前
JQuick-Excel VALIDATION 实战:用作用范围约束 Excel 导入校验
开发语言·python·excel
小静AI工程实验室9 小时前
Python 爬虫解析 JSON-LD:多块 script、@graph 与坏数据的 9 个边界
爬虫·python·json
jason.zeng@15022079 小时前
(八)现有架构上新增一个通用Excel导出工具
python·架构·langchain·excel·llama
袁袁袁袁满9 小时前
AI Agent 如何“看懂“互联网?
爬虫·python·自动化·爬虫实战·多线程爬虫
泡海椒10 小时前
JQuick-Excel 实战:用 VALIDATION 建立可维护的 Excel 导入校验
开发语言·python·excel
Duang007_10 小时前
生产可观测性:从“系统慢“到“根因“的完整链路(Go / TypeScript)
后端·python·golang·typescript·prometheus
jason.zeng@150220710 小时前
(九)多轮对话式新增维修记录实现方案
python·prompt·交互·llama
黑妹天下第一乖10 小时前
小智改造实战解读-首 token 延迟去哪了:云端与端侧大模型的分段对照
开发语言·人工智能·python·嵌入式硬件·自然语言处理·iot
坊钰10 小时前
【LangChain框架入门级】10. 文本向量与向量数据库(Embedding / Redis / Pinecone / MMR)
数据库·python·langchain·embedding