Table of Contents
极简版
- JSON 美化/校验:
python3 -m json.tool data.json - Base64 编解码:
echo "Hi" | python3 -m base64;解码-d - 代码微基准:
python3 -m timeit '[i for i in range(1000)]' - 性能分析:
python3 -m cProfile -s time your_script.py - ZIP 压缩/解压:
python3 -m zipfile -c out.zip a.txt b.txt;解压-e - Python 之禅:
python3 -m this - 反重力彩蛋:
python3 -m antigravity - 终端日历:
python3 -m calendar 2026
python -m http.server 确实是 Python 标准库(Standard Library)中“一行流”工具的经典代表。它利用了 Python 的 -m 参数,将库当作脚本来执行。
Python 的标准库里藏着很多像瑞士军刀一样好用的工具,不需要安装任何第三方包,开箱即用。
这里为你整理了一些最实用、最有趣且灵活的 python -m 模块用法:
1. 数据处理与格式化
JSON 美化神器:json.tool
当你拿到一坨乱糟糟的 JSON 数据时,不需要找在线工具,直接用这个。它不仅能格式化(Pretty Print),还能校验 JSON 语法是否正确。
-
用法:
Bash
# 假设你有一个 data.json 文件 python3 -m json.tool data.json # 或者结合管道命令(curl 的结果直接美化) echo '{"name": "Gemini", "type": "AI"}' | python3 -m json.tool
Base64 编解码:base64
快速对字符串或文件进行 Base64 编码/解码,不用写脚本。
-
用法:
Bash
# 编码 echo "Hello World" | python3 -m base64 # 解码 (-d) echo "SGVsbG8gV29ybGQK" | python3 -m base64 -d
图片编码
python3 -m base64 -e logo.png > logo_b64.txt
2. 性能分析与调试
代码跑分王:timeit
想知道一行代码执行得有多快?或者比较两种写法哪个效率高?timeit 是最权威的微基准测试工具。
-
用法:
Bash
# 比如:测试生成列表,列表推导式 vs 普通循环 python3 -m timeit '[i for i in range(1000)]'
性能分析器:cProfile
如果你的 Python 脚本运行很慢,想知道时间都花在哪个函数上了,用这个可以直接生成分析报告。
-
用法:
Bash
# 直接运行脚本并输出性能统计,按时间排序 (-s time) python3 -m cProfile -s time your_script.py
3. 文件与系统管理
压缩/解压工具:zipfile / tarfile
在一个没有安装 zip 或 tar 命令的精简 Linux 环境(如某些 Docker 容器)中,只要有 Python,就能处理压缩包。
-
用法:
Bash
# 创建压缩包 (-c create) python3 -m zipfile -c output.zip file1.txt file2.txt # 解压压缩包 (-e extract) python3 -m zipfile -e output.zip target_dir/
4. 趣味与彩蛋 (Easter Eggs)
Python 社区以幽默感著称,标准库里也有不少彩蛋。
Python 之禅:this
展示 Python 的核心设计哲学。
-
用法:
Bash
python3 -m this
反重力漫画:antigravity
这会直接打开浏览器跳转到那幅著名的 XKCD 漫画,解释了为什么要学习 Python。
-
用法:
Bash
python3 -m antigravity
日历:calendar
在终端快速打印日历,有点像 Linux 的 cal 命令。
-
用法:
Bash
# 打印 2026 年日历 python3 -m calendar 2026
咱们的社群
星球中有源码、解决方案、提示词,以及落地经验.
欢迎加入我们,思考技术对商业的价值.

欢迎找我合作
AI咨询、AI项目陪跑、AI项目落地.
我的微信: leigeaicom
大家一起来讨论