# Python CLI Tools Python CLI Tools 原文 Python 命令行实用程序 Python's many command-line utilities https://www.pythonmorsels.com/cli-tools/ 部分翻译和笔记如下 ## 原理 使用 `-m` 选项时,是告诉解释器以模块模式运行代码。`-m` 后面跟着的是一个模块的名称,Python 解释器会将这个模块作为脚本执行。 常见用法是运行标准库中的脚本或工具,比如运行 unittest ,启动 Python 的内置单元测试框架。 ## 模块 ### 常用工具 |命令|目的|更多| |---|---|---| |`python -m http.server`|启动简单的 Web 服务器|[视频](https://www.pythonmorsels.com/http-server/)| |`python -m webbrowser`|启动 Web 浏览器|[文档](https://docs.python.org/3/library/webbrowser.html#command-line-interface)| |`python -m json.tool`|格式化 JSON 数据|[文档](https://docs.python.org/3/library/json.html#module-json.tool)| |`python -m calendar`|显示命令行日历|[文档](https://docs.python.org/3/library/calendar.html#command-line-usage)| 示例 ``` python -m http.server Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ... python -m webbrowser www.ftls.xyz python -m json.tool /home/some.json python -m calendar 2024 07 July 2024 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ``` 命令|目的|更多| |---|---|---| |`python -m uuid`|`uuidgen`|[文档](https://docs.python.org/3/library/uuid.html#command-line-usage)| |`python -m sqlite3`|`sqlite3`|[文档](https://docs.python.org/3/library/sqlite3.html#command-line-interface)| |`python -m zipfile`|`zip``unzip`|[文档](https://docs.python.org/3/library/zipfile.html#command-line-interface)| |`python -m gzip`|`gzip``gunzip`|[文档](https://docs.python.org/3/library/gzip.html#command-line-interface)| |`python -m tarfile`| `tar`|[文档](https://docs.python.org/3/library/tarfile.html#command-line-interface)| |`python -m base64`| `base64`|| |`python -m ftplib`|`ftp`|| |`python -m smtplib`|`sendmail`|| |`python -m poplib`|阅读电子邮件`curl`|| |`python -m imaplib`|阅读电子邮件`curl`|| |`python -m telnetlib`|`telnet`| ### 处理 Python 代码 |命令|目的|更多| |---|---|---| |`python -m pip`|安装第三方 Python 包|[文档](https://docs.python.org/3/installing/index.html)| |`python -m venv`|创建虚拟环境|[文档](https://docs.python.org/3/library/venv.html)| |`python -m pdb`|运行 Python 调试器|[文档](https://docs.python.org/3/library/pdb.html)| |`python -m unittest`|在目录中运行测试`unittest`|[文档](https://docs.python.org/3/library/unittest.html#command-line-interface)| |`python -m pydoc`|显示给定字符串的文档|[文档](https://docs.python.org/3/library/pydoc.html)| |`python -m doctest`|对给定的 Python 文件运行 doctests|[文档](https://docs.python.org/3/library/doctest.html)| |`python -m ensurepip`|如果未安装,请安装`pip`|[文档](https://docs.python.org/3/library/ensurepip.html#command-line-interface)| |`python -m idlelib`|启动 Python 的 IDLE 图形 REPL|[文档](https://docs.python.org/3/library/idle.html)| |`python -m zipapp`|将 Python 模块转换为可运行的 ZIP|[文档](https://docs.python.org/3/library/zipapp.html#command-line-interface)| |`python -m compileall`|将 Python 文件预编译为字节码|[文档](https://docs.python.org/3/library/compileall.html)| - pip 安装第三方 Python 包。 - venv 创建虚拟环境。 - pdb 为 Python 调试器提供支持。 - unittest 用于在 Python 中编写自动化测试。 当作为命令行脚本运行时,将识别当前目录中的所有测试并自动运行。 - pydoc 将模块作为命令行脚本运行将显示给定模块或对象的文档。 这与将相同的对象名称传递给内置函数时看到的文档相同。 - doctest 作为命令行脚本运行将评估给定 Python 文件中的所有 doctest(文档字符串中的示例代码)。 - ensurepip 用于发现已卸载并需要重新安装它的方法的人 - idlelib 命令行启动 Python 的图形 IDLE 工具。 - zipapp 将 Python 模块捆绑成一个可以由 Python 直接运行的 ZIP 文件。 `python -m zipapp my_module` - compileall 预热 Python 用于运行模块的已编译字节码缓存。运行以将当前目录下的所有 Python 文件编译为缓存字节码。`python -m compileall .` ### 分析 Python 代码 |命令|目的|更多| |---|---|---| |`python -m tokenize`|将 Python 模块分解为“令牌”|[文档](https://docs.python.org/3/library/tokenize.html#command-line-usage)| |`python -m ast`|显示代码的抽象语法树|[文档](https://docs.python.org/3/library/ast.html#command-line-usage)| |`python -m dis`|将 Python 代码反汇编为字节码|[文档](https://docs.python.org/3/library/dis.html#command-line-interface)| |`python -m inspect`|检查 Python 对象的源代码|[文档](https://docs.python.org/3/library/inspect.html#command-line-interface)| |`python -m pyclbr`|查看模块对象概述| ### 彩蛋与其他 |命令|目的| |---|---| |`python -m __hello__`|打印`Hello world!`| |`python -m this`|展示 Python 的禅意 (PEP 20)| |`python -m antigravity`|在 Web 浏览器中打开 XKCD 353| |`python -m turtledemo`|查看模块演示`turtle`| Python 之禅 The Zen of Python, by Tim Peters > > Beautiful is better than ugly. > Explicit is better than implicit. > Simple is better than complex. > Complex is better than complicated. > Flat is better than nested. > Sparse is better than dense. > Readability counts. > Special cases aren't special enough to break the rules. > Although practicality beats purity. > Errors should never pass silently. > Unless explicitly silenced. > In the face of ambiguity, refuse the temptation to guess. > There should be one-- and preferably only one --obvious way to do it. > Although that way may not be obvious at first unless you're Dutch. > Now is better than never. > Although never is often better than *right* now. > If the implementation is hard to explain, it's a bad idea. > If the implementation is easy to explain, it may be a good idea. > Namespaces are one honking great idea -- let's do more of those! 翻译 https://www.cnblogs.com/huangbiquan/p/7881913.html > Python之禅 by Tim Peters > > 优美胜于丑陋(Python 以编写优美的代码为目标) > 明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似) > 简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现) > 复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁) > 扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套) > 间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题) > 可读性很重要(优美的代码是可读的) > 即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上) > > 不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写 except:pass 风格的代码) > > 当存在多种可能,不要尝试去猜测 > 而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法) > 虽然这并不容易,因为你不是 Python 之父(这里的 Dutch 是指 Guido ) > > 做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量) > > 如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准) > > 命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召) ### 其他工具 |命令|目的|更多| |---|---|---| |`python -m asyncio`|启动异步感知 Python REPL|[文档](https://docs.python.org/3/library/asyncio.html)| |`python -m cProfile`|分析 Python 程序|[文档](https://docs.python.org/3/library/profile.html)| |`python -m profile`|使用纯 Python 分析 Python 程序|| |`python -m pstats`|显示配置文件/c配置文件生成的文件的统计信息|| |`python -m pickle`|显示 pickle 文件的内容(高级)|[文档](https://docs.python.org/3/library/pickle.html)| |`python -m pickletools`|反汇编 pickle 文件(低级)|[文档](https://docs.python.org/3/library/pickletools.html)| |命令|目的| |---|---| |`python -m code`|运行 Python REPL| |`python -m runpy`|将 Python 模块作为脚本运行| |命令|目的| |---|---| |`python -m timeit`|对 Python 表达式进行计时| |`python -m site`|请参阅有关 Python 的“站点”信息| |`python -m sysconfig`|显示 Python 配置详细信息| |`python -m platform`|显示当前平台信息| |`python -m mimetypes`|显示文件 mimetype/extension details| |`python -m quopri`|对原始电子邮件数据进行编码/解码| |`python -m filecmp`|比较 2 个目录的内容| |`python -m encodings.rot_13`|ROT-13 编码/解码文本| |`python -m tabnanny`|检查 Python 文件中是否有混合的制表符和空格| ## 总结 |Module/Script|用途|类别| |---|---|---| |`http.server`|启动一个简单的Web服务器|通用| |`webbrowser`|启动您的网络浏览器|通用| |`json.tool`|美观地格式化JSON数据|通用| |`calendar`|显示命令行日历|通用| |`uuid`|类似于`uuidgen` CLI工具|类Linux| |`sqlite3`|类似于`sqlite3` CLI工具|类Linux| |`zipfile`|类似于`zip`和`unzip` CLI工具|类Linux| |`gzip`|类似于`gzip`和`gunzip` CLI工具|类Linux| |`tarfile`|类似于`tar` CLI工具|类Linux| |`base64`|类似于`base64` CLI工具|类Linux| |`ftplib`|类似于`ftp`工具|类Linux| |`smtplib`|类似于`sendmail`工具|类Linux| |`poplib`|类似于使用`curl`读取邮件|类Linux| |`imaplib`|类似于使用`curl`读取邮件|类Linux| |`telnetlib`|类似于`telnet`工具|类Linux| |`pip`|安装第三方Python包|Python| |`venv`|创建虚拟环境|Python| |`pdb`|运行Python调试器|Python| |`unittest`|在目录中运行`unittest`测试|Python| |`pydoc`|显示给定字符串的文档|Python| |`doctest`|为给定的Python文件运行doctest|Python| |`ensurepip`|如果未安装,则安装`pip`|Python| |`idlelib`|启动Python的IDLE图形REPL|Python| |`zipapp`|将Python模块转换为可运行的ZIP|Python| |`python -m compileall`|预编译Python文件到字节码|Python| |`tokenize`|将Python模块分解为“令牌”|检查代码| |`ast`|显示代码的抽象语法树|检查代码| |`dis`|将Python代码反汇编为字节码|检查代码| |`inspect`|检查Python对象的源代码|检查代码| |`pyclbr`|查看模块对象的概览|检查代码| |`asyncio`|启动异步IO感知的REPL|深入Python| |`cProfile`|分析Python程序|深入Python| |`profile`|用Python分析Python程序|深入Python| |`pstats`|显示由cProfile生成的文件的统计信息|深入Python| |`pickle`|以可读方式显示pickle文件内容|深入Python| |`pickletools`|反汇编pickle文件|深入Python| |`tabnanny`|检查文件中的混合制表符和空格|深入Python| |`this`|显示Python之禅(PEP 20)|乐趣| |`__hello__`|打印“Hello world!”|乐趣| |`antigravity`|在Web浏览器中打开XKCD 353|乐趣| |`turtledemo`|查看`turtle`模块演示|乐趣| |`code`|运行Python REPL|Python| |`runpy`|作为脚本运行Python模块|Python| |`timeit`|计时Python表达式|Python| |`site`|查看关于Python的“站点”信息|深入Python| |`sysconfig`|显示Python配置详细信息|深入Python| |`platform`|显示当前平台信息|通用| |`mimetypes`|显示文件MIME类型/扩展名详情|通用| |`quopri`|编码/解码原始电子邮件数据|通用| |`filecmp`|比较两个目录的内容|通用| |`encodings.rot_13`|ROT-13编码/解码文本|通用|