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.0.tar.gz (11.7 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.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: znpy-0.2.0.tar.gz
  • Upload date:
  • Size: 11.7 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.0.tar.gz
Algorithm Hash digest
SHA256 234a6a65f9c02d1eac8051e02754d7eab95d67baa72fb164c5b41d301619a875
MD5 f9500473f699f6ed31c44d9b407f9c4c
BLAKE2b-256 2f80869bbeaa88f47339141a289fa17c3c49c8ad72b48609a0ab60f9715fb08a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: znpy-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb654ac3fd429bc07a4ac68e2ae236334c784f91a34c2da368f9dd68841ef9c4
MD5 88736c01a397383dfb3363a4b7f1dcc5
BLAKE2b-256 29a8427860509de169acb0383c1ff4f9c200f7db0207c38722f276e20ae39af0

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