Skip to main content

中文 Python 错误信息工具 - 将 Python 错误信息翻译为中文

Project description

Znpy — 中文 Python 错误信息工具

归属于: https://gitee.com/xnors

将 Python 报错信息翻译为中文,专为 Python 初学者设计。支持脚本运行、交互式 Shell 以及所有 Python 标准选项(-c-m 等),兼容 pip 安装。

安装

# 通过 pip 安装
pip install znpy

# 通过 uv 全局安装
uv tool install znpy

快速开始

运行 Python 脚本,中文报错

znpy main.py

进入交互式 Shell

znpy

在 Shell 中输入的代码如果报错,将显示中文翻译。

执行单行命令

znpy -c "1/0"

作为模块运行

znpy -m http.server 8000

示例

运行脚本错误

znpy test.py

输出:

==================================================
  中文错误报告 (Chinese Error Report)
==================================================

  文件: test.py
  行号: 2
  位置: 函数 divide
  代码: return a / b

--------------------------------------------------
  ❌ 除零错误: division by zero
--------------------------------------------------

Shell 交互

支持方向键浏览命令历史、移动光标,提供流畅的交互体验。

$ znpy
==================================================
  Znpy - 中文 Python Shell
  输入 Python 代码,错误信息将以中文显示
  输入 exit()  Ctrl+Z 退出
==================================================

Python 3.13.3 ... on win32
(ChineseConsole)
>>> print(undefined_var)
==================================================
  中文错误报告 (Chinese Error Report)
==================================================

  文件: <console>
  行号: 1
  位置: 顶层模块
  代码: print(undefined_var)

--------------------------------------------------
   名称错误: name 'undefined_var' is not defined
--------------------------------------------------

翻译覆盖范围

  • 70+ 内置异常类型中文名称(NameError名称错误ZeroDivisionError除零错误 等)
  • 120+ 常见错误消息中文翻译(通过正则匹配)
  • 原始堆栈跟踪文本中文翻译Traceback (most recent call last):堆栈跟踪(最近一次调用最后):File "...", line N, in func文件: "...", 第 N 行, 位于 func 等)
  • 堆栈跟踪(Traceback)完整中文格式化
  • 语法错误(SyntaxError)详细中文展示

扩展翻译

~/.znpy/extensions/ 目录下创建 .py 文件,定义以下变量即可添加自定义翻译:

# ~/.znpy/extensions/my_ext.py

# 自定义异常中文名
EXCEPTION_ZH = {
    "MyCustomError": "我的自定义错误",
}

# 自定义错误消息模板 (支持 {msg} 占位符)
EXCEPTION_TEMPLATES = {
    "MyCustomError": "自定义错误: {msg}",
}

# 自定义正则翻译模式
PATTERNS = [
    (r"my (.+) pattern", "我的 {0} 模式"),
]

项目结构

znpy/
├── pyproject.toml              # 项目构建配置 (hatchling)
├── src/
│   └── znpy/
│       ├── __init__.py         # 包入口
│       ├── __main__.py         # CLI 入口
│       ├── runner.py           # 脚本/模块执行引擎
│       ├── shell.py            # 交互式 Shell
│       ├── translator.py       # 翻译核心引擎
│       └── errors/
│           ├── __init__.py
│           ├── builtins.py     # 内置异常中文名 + 模板
│           ├── common.py       # 常见错误消息正则匹配
│           └── ext.py          # 扩展加载器

构建与发布

# 安装构建依赖
uv pip install build

# 构建分发包
python -m build

# 发布到 PyPI
twine upload dist/*

开源协议

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

znpy-0.2.1.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

znpy-0.2.1-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file znpy-0.2.1.tar.gz.

File metadata

  • Download URL: znpy-0.2.1.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for znpy-0.2.1.tar.gz
Algorithm Hash digest
SHA256 2ec2df4bb4436cef6dabdc7b91148d5f9997ac8b3e28f1d2ef025763b0ea4e13
MD5 5e82db698f73e84d4753735d485e6ab6
BLAKE2b-256 d8c5a6a4b66b04a4ac1e3e5fafd8e328bfb57644d94fc00546a5bc854c12e75e

See more details on using hashes here.

File details

Details for the file znpy-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: znpy-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for znpy-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2e496902d6fd5399e41cf733bee3dfc1ef071e0873714dc1a3645dce0c7ea102
MD5 097b76f0f247598208e69579158924bb
BLAKE2b-256 91e3eb1f336fccdd1f8dfa7d8885cb7d4bb37ca73ef5775ad590a33b3d1767f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page