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
--------------------------------------------------

翻译覆盖范围

  • 60+ 内置异常类型中文名称(NameError名称错误ZeroDivisionError除零错误 等)
  • 60+ 常见错误消息中文翻译(通过正则匹配)
  • 堆栈跟踪(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.1.0.tar.gz (9.4 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.1.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for znpy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9b1ada5280cfe6cb771135b4cf18799f1d5679b65d74130357683754d2e45c9b
MD5 c853036df031ef7e7c511a4c6960e5b0
BLAKE2b-256 549437025268ae3b4c6672973121889bc6e232accf4504e48a2cdd7715b56853

See more details on using hashes here.

File details

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

File metadata

  • Download URL: znpy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.1 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d573b379094880bfafcb92ee4d0069d5933cfeb24f39505db908a9386c109ad
MD5 d493c8e74f977e2c78c6dfe915e5d556
BLAKE2b-256 d662dcffe45694428f48b925405823001e2fca1ccf02fb06bb9e30ddd95db0cf

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