py2py3-converter:Python 2 转 3 迁移工具 - Openclaw Skills

作者:互联网

2026-04-14

AI教程

什么是 py2py3-converter?

py2py3-converter 是一个核心的 Openclaw Skills 扩展,旨在弥合旧版环境与现代开发标准之间的差距。由于 Python 2 已正式停止维护,开发者经常面临手动重构的艰巨任务。此技能可自动处理迁移中最繁琐的部分,确保语法、类型处理和标准库导入均正确更新至 Python 3 标准。

通过将此工具集成到您的工作流程中,您可以处理复杂的转换,例如 print 函数转换、整数除法调整以及删除已弃用的导入。它为现代化提供了一个稳健的框架,使其成为在 Openclaw Skills 市场中维护长期项目的团队的基石。

下载入口:https://github.com/openclaw/skills/tree/main/skills/martinforsulu/neo-py2py3-converter

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install neo-py2py3-converter

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 neo-py2py3-converter。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

py2py3-converter 应用场景

  • 将企业旧版应用程序从 Python 2.7 现代化到 3.x。
  • 在云迁移过程中自动清理旧脚本中的技术债。
  • 生成自动化单元测试,以确保代码转换后的功能一致性。
  • 在执行全面重构之前,审核代码库是否存在 Python 3 兼容性问题。
py2py3-converter 工作原理
  1. 用户提供旧版 Python 2 代码或指定要处理的源文件。
  2. Openclaw Skills 运行器调用转换逻辑,将 xrange 和 raw_input 等已弃用的语法映射到现代等效项。
  3. 系统执行兼容性扫描,以识别可能需要人工干预的边缘情况。
  4. 如果启用,该技能将生成配套的 pytest 文件,以验证转换后代码的语法和行为完整性。
  5. 向用户返回包含现代化代码和已识别问题的综合报告。

py2py3-converter 配置指南

要开始使用此技能,请确保已配置必要的环境并运行以下命令:

# 安装必要的依赖项
npm install

# 将特定文件从 Python 2 转换为 Python 3
node scripts/cli.js convert --input path/to/legacy.py --output path/to/modern.py

# 运行带有自动化测试生成的转换器
node scripts/cli.js convert --input file.py --output converted.py --generate-tests

py2py3-converter 数据架构与分类体系

该技能通过结构化管道管理数据,输出代码和元数据。下表描述了主要数据组件:

组件 描述
convertedCode 最终的 Python 3 兼容源代码字符串。
issues 包含转换信息、警告和错误详细信息的对象集合。
testFiles 用于功能验证的自动生成 pytest 文件。
exitCodes 标准化返回值(0 表示成功,1 表示警告,2 表示错误)。
name: py2py3-converter
description: Automatically converts legacy Python 2 code to Python 3 with compatibility checks and test generation.
version: 1.0.0
triggers:
  - "Convert this Python 2 code to Python 3"
  - "Upgrade this legacy script to Python 3"
  - "Fix Python 2 to 3 compatibility issues"
  - "Automate Python 2 to 3 migration"
  - "Generate tests for Python 3 converted code"
  - "Transform this script using 2to3"

py2py3-converter

1. Introduction

py2py3-converter is an OpenClaw skill that automatically converts legacy Python 2 code to modern Python 3 syntax. It handles the most common migration patterns, generates compatibility reports, and creates unit tests for the converted code.

Python 2 reached end-of-life on January 1, 2020, yet many codebases still contain Python 2 code. This skill automates the tedious work of manual migration, reducing human error and accelerating the upgrade process.

2. Core Capabilities

Conversion Logic

The converter handles these Python 2 → 3 transformations:

  • print statements → print() function calls
  • raw_input()input()
  • xrange()range()
  • unicode()str()
  • basestringstr
  • long type → int
  • dict.has_key(k)k in dict
  • except Exception, eexcept Exception as e
  • raise ValueError, "msg"raise ValueError("msg")
  • Integer division / awareness
  • __future__ import removal
  • iteritems() / itervalues() / iterkeys()items() / values() / keys()
  • reduce()functools.reduce()

Compatibility Checking

After conversion, a compatibility report is generated listing:

  • Warnings for patterns that may need manual review
  • Errors for constructs that could not be safely converted
  • Informational notes about behavior changes

Test Generation

For each converted file, a companion pytest test file is generated that:

  • Validates syntax correctness of converted code
  • Tests key functions for expected behavior
  • Covers detected edge cases

3. Edge Cases Handling

  • print "hello"print("hello")
  • print "a", "b"print("a", "b")
  • print >> sys.stderr, "err"print("err", file=sys.stderr)
  • print() already valid → left unchanged

String/Bytes Compatibility

  • u"string" prefix → "string" (default in Python 3)
  • Warns about b"bytes" patterns that may need review
  • basestring references → str

Import Resolution

  • from __future__ import print_function → removed (default in Python 3)
  • import urllib2import urllib.request
  • import ConfigParserimport configparser
  • reduce → adds from functools import reduce

4. CLI Interface

Command Syntax

# Convert a file
node scripts/cli.js convert --input path/to/py2file.py --output path/to/py3file.py

# Convert from stdin
cat py2file.py | node scripts/cli.js convert

# Convert and generate tests
node scripts/cli.js convert --input file.py --output converted.py --generate-tests

# Show compatibility report only
node scripts/cli.js check --input file.py

Exit Codes

  • 0 - Conversion successful, no errors
  • 1 - Conversion completed with warnings
  • 2 - Conversion failed due to errors

5. Integration

OpenClaw Agent Workflow

Agents invoke the skill via the CLI interface. The standard workflow:

  1. Agent receives Python 2 code from user
  2. Agent calls node scripts/cli.js convert --input
  3. Skill returns converted code + compatibility report
  4. Agent presents results to user

API Contract

Input:

{
  "code": "print 'hello world'",
  "options": {
    "generateTests": false,
    "keepWarnings": true
  }
}

Output:

{
  "convertedCode": "print('hello world')",
  "issues": [
    {
      "type": "info",
      "line": 1,
      "message": "Converted print statement to function call"
    }
  ]
}