claude-code-python-wrapper


Nameclaude-code-python-wrapper JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryA comprehensive Python wrapper for Claude CLI with async support
upload_time2025-07-17 13:46:01
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords claude cli wrapper ai async translation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Claude Code Python Wrapper

一个功能完整的 Python 包装器,用于调用本地 Claude CLI (`claudee`) 命令。支持同步和异步操作,提供简洁的 API 接口。

[![Python Version](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## 特性

- 🚀 **双模式支持**:同步 (`ClaudeCLI`) 和异步 (`AsyncClaudeCLI`) 接口
- 🔄 **流式响应**:支持实时流式输出(异步模式)
- 💬 **交互式会话**:保持上下文的多轮对话
- 📊 **JSON 解析**:自动解析 JSON 格式响应
- ⚙️ **灵活配置**:丰富的选项定制 Claude 行为
- 🛡️ **错误处理**:完善的错误处理和超时控制
- ⚡ **并发处理**:支持批量并发请求

## 安装

### 方法 1:从 GitHub 安装(推荐)

```bash
# 直接从 GitHub 安装
pip install git+https://github.com/jancee/claude-code-python-wrapper.git

# 或者使用 SSH
pip install git+ssh://git@github.com/jancee/claude-code-python-wrapper.git
```

### 方法 2:克隆后本地安装

```bash
# 克隆项目
git clone https://github.com/jancee/claude-code-python-wrapper.git
cd claude-code-python-wrapper

# 安装包
pip install .

# 或者开发模式安装(推荐开发者使用)
pip install -e .

# 安装开发依赖(可选)
pip install -e ".[dev]"
```

### 方法 3:下载后安装

```bash
# 下载并解压代码
wget https://github.com/jancee/claude-code-python-wrapper/archive/main.zip
unzip main.zip
cd claude-code-python-wrapper-main

# 安装
pip install .
```

### 依赖要求

- Python >= 3.10
- `claudee` 命令已安装并可在 PATH 中访问

## 快速开始

### 基础用法

```python
from claude_cli import ClaudeCLI

# 初始化包装器
claude = ClaudeCLI(command="claudee")

# 简单查询
response = claude.query("什么是 Python?")
print(response.output)

# 检查执行状态
if response.return_code == 0:
    print("成功!")
else:
    print(f"错误:{response.error}")
```

### 带选项的查询

```python
from claude_cli import ClaudeCLI, ClaudeOptions

# 配置选项
options = ClaudeOptions(
    max_turns=3,
    model="claude-3-sonnet",
    max_tokens=500,
    system_prompt="你是一个专业的 Python 开发者"
)

# 执行查询
response = claude.query("编写一个快速排序算法", options=options)
```

### 异步操作

```python
import asyncio
from claude_cli import AsyncClaudeCLI

async def main():
    claude = AsyncClaudeCLI(command="claudee")
    
    # 单个异步查询
    response = await claude.query("解释 async/await")
    print(response.output)
    
    # 并发执行多个查询
    queries = ["什么是列表推导?", "什么是装饰器?", "什么是生成器?"]
    tasks = [claude.query(q) for q in queries]
    responses = await asyncio.gather(*tasks)
    
    for query, response in zip(queries, responses):
        print(f"Q: {query}")
        print(f"A: {response.output[:100]}...\n")

asyncio.run(main())
```

### 流式响应

```python
async def stream_example():
    claude = AsyncClaudeCLI(command="claudee")
    
    # 实时获取流式输出
    async for line in claude.stream_query("写一个关于编程的故事"):
        print(line, end='', flush=True)
```

### 交互式会话

```python
# 同步交互
claude = ClaudeCLI()
proc = claude.interactive(initial_prompt="我们来讨论 Python")

# 异步交互
async def interactive_chat():
    claude = AsyncClaudeCLI()
    
    async with await claude.interactive_session() as session:
        response = await session.send("什么是面向对象编程?")
        print(response)
        
        response = await session.send("给我一个例子")
        print(response)
```

## API 参考

### ClaudeOptions 配置项

| 参数 | 类型 | 描述 |
|------|------|------|
| `max_turns` | `int` | 最大对话轮数 |
| `system_prompt` | `str` | 系统提示词 |
| `model` | `str` | 使用的模型名称 |
| `temperature` | `float` | 响应随机性 (0.0-1.0) |
| `max_tokens` | `int` | 最大响应长度 |
| `allowed_tools` | `List[str]` | 允许使用的工具列表 |
| `permission_mode` | `str` | 权限模式 |
| `cwd` | `str` | 工作目录 |
| `extra_args` | `List[str]` | 额外的命令行参数 |

### ClaudeResponse 响应对象

| 属性 | 类型 | 描述 |
|------|------|------|
| `output` | `str` | Claude 的响应内容 |
| `return_code` | `int` | 命令返回码 (0 表示成功) |
| `error` | `str` | 错误信息(如果有) |
| `metadata` | `dict` | 额外的元数据信息 |

## 高级用法

### 批量处理与并发控制

```python
import asyncio
from claude_cli import AsyncClaudeCLI

async def batch_process_with_limit():
    claude = AsyncClaudeCLI()
    
    # 准备任务列表
    tasks = [
        {"id": 1, "prompt": "解释 Python 的 GIL"},
        {"id": 2, "prompt": "什么是元类?"},
        {"id": 3, "prompt": "解释描述符协议"},
        # ... 更多任务
    ]
    
    # 使用信号量限制并发数
    semaphore = asyncio.Semaphore(3)  # 最多 3 个并发请求
    
    async def process_with_limit(task):
        async with semaphore:
            response = await claude.query(task["prompt"])
            return {"id": task["id"], "result": response.output}
    
    # 并发处理所有任务
    results = await asyncio.gather(*[process_with_limit(t) for t in tasks])
    return results
```

### 错误处理与重试

```python
import asyncio
from claude_cli import AsyncClaudeCLI

async def query_with_retry(prompt, max_retries=3):
    claude = AsyncClaudeCLI()
    
    for attempt in range(max_retries):
        try:
            response = await claude.query(prompt, timeout=10.0)
            if response.return_code == 0:
                return response
            
            # 如果是临时错误,等待后重试
            if attempt < max_retries - 1:
                await asyncio.sleep(2 ** attempt)  # 指数退避
                
        except Exception as e:
            print(f"尝试 {attempt + 1} 失败: {e}")
            if attempt == max_retries - 1:
                raise
    
    return None
```

### 自定义日志

```python
import logging
from claude_cli import ClaudeCLI

# 配置日志
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

# 创建自定义 logger
logger = logging.getLogger("my_app")

# 使用自定义 logger
claude = ClaudeCLI(logger=logger)
```

## 实际应用示例

### 代码审查助手

```python
from claude_cli import ClaudeCLI

class CodeReviewer:
    def __init__(self):
        self.claude = ClaudeCLI()
    
    def review_code(self, code: str, language: str = "python"):
        prompt = f"""请审查以下 {language} 代码:
        
```{language}
{code}
```

提供:
1. 潜在的 bug 或问题
2. 性能优化建议
3. 代码风格改进
4. 安全性考虑
"""
        response = self.claude.query(prompt)
        return response.output if response.return_code == 0 else None

# 使用示例
reviewer = CodeReviewer()
code = """
def get_user(user_id):
    return db.query(f"SELECT * FROM users WHERE id = {user_id}")
"""
review = reviewer.review_code(code)
print(review)
```

### 文档生成器

```python
async def generate_docs(file_path: str):
    claude = AsyncClaudeCLI()
    
    with open(file_path, 'r') as f:
        code = f.read()
    
    prompt = f"""为以下代码生成详细的文档字符串:

{code}

要求:
- 使用 Google 风格的文档字符串
- 包含参数说明和返回值
- 添加使用示例
"""
    
    response = await claude.query(prompt)
    return response.output
```

## 运行示例

项目包含多个示例脚本:

```bash
# 基础示例
python examples/basic_usage.py

# 异步示例
python examples/async_usage.py

# 高级用法
python examples/advanced_usage.py

# 测试脚本
python test_basic.py
python test_async.py
python code_analyzer.py
```

## 故障排除

### 常见问题

1. **"Claude CLI 'claudee' not found in PATH"**
   - 确保 `claudee` 命令已正确安装
   - 检查 PATH 环境变量
   - 尝试使用完整路径初始化:`ClaudeCLI(command="/full/path/to/claudee")`

2. **超时错误**
   - 增加 timeout 参数:`claude.query(prompt, timeout=30.0)`
   - 检查网络连接
   - 考虑使用异步模式处理长时间运行的查询

3. **JSON 解析错误**
   - 确保 Claude 返回的是有效的 JSON 格式
   - 使用 try-except 处理解析错误

### 调试技巧

```python
# 启用详细日志
import logging
logging.basicConfig(level=logging.DEBUG)

# 查看完整命令
response = claude.query("test")
print(response.metadata["command"])  # 显示实际执行的命令
```

## 项目结构

```
claude-cli-wrapper/
├── claude_cli/
│   ├── __init__.py        # 包导出
│   ├── wrapper.py         # 同步包装器实现
│   └── async_wrapper.py   # 异步包装器实现
├── examples/
│   ├── basic_usage.py     # 基础用法示例
│   ├── async_usage.py     # 异步操作示例
│   └── advanced_usage.py  # 高级功能示例
├── tests/                 # 测试目录
├── pyproject.toml         # 项目配置
├── README.md             # 本文档
├── test_basic.py         # 基础测试脚本
├── test_async.py         # 异步测试脚本
└── code_analyzer.py      # 代码分析器示例
```

## 贡献指南

欢迎贡献!请遵循以下步骤:

1. Fork 项目
2. 创建功能分支 (`git checkout -b feature/amazing-feature`)
3. 提交更改 (`git commit -m 'Add amazing feature'`)
4. 推送到分支 (`git push origin feature/amazing-feature`)
5. 创建 Pull Request

### 开发要求

- 遵循 PEP 8 代码风格
- 添加适当的类型注解
- 编写单元测试
- 更新文档

## 许可证

MIT License - 详见 [LICENSE](LICENSE) 文件

## 作者

- jancee (@jancee)

## 致谢

- 感谢 Anthropic 提供 Claude CLI 工具
- 感谢所有贡献者

---

如有问题或建议,请提交 [Issue](https://github.com/jancee/claude-code-python-wrapper/issues) 或 [Pull Request](https://github.com/jancee/claude-code-python-wrapper/pulls)。

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "claude-code-python-wrapper",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "claude, cli, wrapper, ai, async, translation",
    "author": null,
    "author_email": "jancee <jancee@example.com>",
    "download_url": "https://files.pythonhosted.org/packages/08/5a/d8821c22163e49dde0a06e5182e5967de0270c6b96dfcfa593fbc7381c8f/claude_code_python_wrapper-0.1.0.tar.gz",
    "platform": null,
    "description": "# Claude Code Python Wrapper\n\n\u4e00\u4e2a\u529f\u80fd\u5b8c\u6574\u7684 Python \u5305\u88c5\u5668\uff0c\u7528\u4e8e\u8c03\u7528\u672c\u5730 Claude CLI (`claudee`) \u547d\u4ee4\u3002\u652f\u6301\u540c\u6b65\u548c\u5f02\u6b65\u64cd\u4f5c\uff0c\u63d0\u4f9b\u7b80\u6d01\u7684 API \u63a5\u53e3\u3002\n\n[![Python Version](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n## \u7279\u6027\n\n- \ud83d\ude80 **\u53cc\u6a21\u5f0f\u652f\u6301**\uff1a\u540c\u6b65 (`ClaudeCLI`) \u548c\u5f02\u6b65 (`AsyncClaudeCLI`) \u63a5\u53e3\n- \ud83d\udd04 **\u6d41\u5f0f\u54cd\u5e94**\uff1a\u652f\u6301\u5b9e\u65f6\u6d41\u5f0f\u8f93\u51fa\uff08\u5f02\u6b65\u6a21\u5f0f\uff09\n- \ud83d\udcac **\u4ea4\u4e92\u5f0f\u4f1a\u8bdd**\uff1a\u4fdd\u6301\u4e0a\u4e0b\u6587\u7684\u591a\u8f6e\u5bf9\u8bdd\n- \ud83d\udcca **JSON \u89e3\u6790**\uff1a\u81ea\u52a8\u89e3\u6790 JSON \u683c\u5f0f\u54cd\u5e94\n- \u2699\ufe0f **\u7075\u6d3b\u914d\u7f6e**\uff1a\u4e30\u5bcc\u7684\u9009\u9879\u5b9a\u5236 Claude \u884c\u4e3a\n- \ud83d\udee1\ufe0f **\u9519\u8bef\u5904\u7406**\uff1a\u5b8c\u5584\u7684\u9519\u8bef\u5904\u7406\u548c\u8d85\u65f6\u63a7\u5236\n- \u26a1 **\u5e76\u53d1\u5904\u7406**\uff1a\u652f\u6301\u6279\u91cf\u5e76\u53d1\u8bf7\u6c42\n\n## \u5b89\u88c5\n\n### \u65b9\u6cd5 1\uff1a\u4ece GitHub \u5b89\u88c5\uff08\u63a8\u8350\uff09\n\n```bash\n# \u76f4\u63a5\u4ece GitHub \u5b89\u88c5\npip install git+https://github.com/jancee/claude-code-python-wrapper.git\n\n# \u6216\u8005\u4f7f\u7528 SSH\npip install git+ssh://git@github.com/jancee/claude-code-python-wrapper.git\n```\n\n### \u65b9\u6cd5 2\uff1a\u514b\u9686\u540e\u672c\u5730\u5b89\u88c5\n\n```bash\n# \u514b\u9686\u9879\u76ee\ngit clone https://github.com/jancee/claude-code-python-wrapper.git\ncd claude-code-python-wrapper\n\n# \u5b89\u88c5\u5305\npip install .\n\n# \u6216\u8005\u5f00\u53d1\u6a21\u5f0f\u5b89\u88c5\uff08\u63a8\u8350\u5f00\u53d1\u8005\u4f7f\u7528\uff09\npip install -e .\n\n# \u5b89\u88c5\u5f00\u53d1\u4f9d\u8d56\uff08\u53ef\u9009\uff09\npip install -e \".[dev]\"\n```\n\n### \u65b9\u6cd5 3\uff1a\u4e0b\u8f7d\u540e\u5b89\u88c5\n\n```bash\n# \u4e0b\u8f7d\u5e76\u89e3\u538b\u4ee3\u7801\nwget https://github.com/jancee/claude-code-python-wrapper/archive/main.zip\nunzip main.zip\ncd claude-code-python-wrapper-main\n\n# \u5b89\u88c5\npip install .\n```\n\n### \u4f9d\u8d56\u8981\u6c42\n\n- Python >= 3.10\n- `claudee` \u547d\u4ee4\u5df2\u5b89\u88c5\u5e76\u53ef\u5728 PATH \u4e2d\u8bbf\u95ee\n\n## \u5feb\u901f\u5f00\u59cb\n\n### \u57fa\u7840\u7528\u6cd5\n\n```python\nfrom claude_cli import ClaudeCLI\n\n# \u521d\u59cb\u5316\u5305\u88c5\u5668\nclaude = ClaudeCLI(command=\"claudee\")\n\n# \u7b80\u5355\u67e5\u8be2\nresponse = claude.query(\"\u4ec0\u4e48\u662f Python\uff1f\")\nprint(response.output)\n\n# \u68c0\u67e5\u6267\u884c\u72b6\u6001\nif response.return_code == 0:\n    print(\"\u6210\u529f\uff01\")\nelse:\n    print(f\"\u9519\u8bef\uff1a{response.error}\")\n```\n\n### \u5e26\u9009\u9879\u7684\u67e5\u8be2\n\n```python\nfrom claude_cli import ClaudeCLI, ClaudeOptions\n\n# \u914d\u7f6e\u9009\u9879\noptions = ClaudeOptions(\n    max_turns=3,\n    model=\"claude-3-sonnet\",\n    max_tokens=500,\n    system_prompt=\"\u4f60\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684 Python \u5f00\u53d1\u8005\"\n)\n\n# \u6267\u884c\u67e5\u8be2\nresponse = claude.query(\"\u7f16\u5199\u4e00\u4e2a\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\", options=options)\n```\n\n### \u5f02\u6b65\u64cd\u4f5c\n\n```python\nimport asyncio\nfrom claude_cli import AsyncClaudeCLI\n\nasync def main():\n    claude = AsyncClaudeCLI(command=\"claudee\")\n    \n    # \u5355\u4e2a\u5f02\u6b65\u67e5\u8be2\n    response = await claude.query(\"\u89e3\u91ca async/await\")\n    print(response.output)\n    \n    # \u5e76\u53d1\u6267\u884c\u591a\u4e2a\u67e5\u8be2\n    queries = [\"\u4ec0\u4e48\u662f\u5217\u8868\u63a8\u5bfc\uff1f\", \"\u4ec0\u4e48\u662f\u88c5\u9970\u5668\uff1f\", \"\u4ec0\u4e48\u662f\u751f\u6210\u5668\uff1f\"]\n    tasks = [claude.query(q) for q in queries]\n    responses = await asyncio.gather(*tasks)\n    \n    for query, response in zip(queries, responses):\n        print(f\"Q: {query}\")\n        print(f\"A: {response.output[:100]}...\\n\")\n\nasyncio.run(main())\n```\n\n### \u6d41\u5f0f\u54cd\u5e94\n\n```python\nasync def stream_example():\n    claude = AsyncClaudeCLI(command=\"claudee\")\n    \n    # \u5b9e\u65f6\u83b7\u53d6\u6d41\u5f0f\u8f93\u51fa\n    async for line in claude.stream_query(\"\u5199\u4e00\u4e2a\u5173\u4e8e\u7f16\u7a0b\u7684\u6545\u4e8b\"):\n        print(line, end='', flush=True)\n```\n\n### \u4ea4\u4e92\u5f0f\u4f1a\u8bdd\n\n```python\n# \u540c\u6b65\u4ea4\u4e92\nclaude = ClaudeCLI()\nproc = claude.interactive(initial_prompt=\"\u6211\u4eec\u6765\u8ba8\u8bba Python\")\n\n# \u5f02\u6b65\u4ea4\u4e92\nasync def interactive_chat():\n    claude = AsyncClaudeCLI()\n    \n    async with await claude.interactive_session() as session:\n        response = await session.send(\"\u4ec0\u4e48\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff1f\")\n        print(response)\n        \n        response = await session.send(\"\u7ed9\u6211\u4e00\u4e2a\u4f8b\u5b50\")\n        print(response)\n```\n\n## API \u53c2\u8003\n\n### ClaudeOptions \u914d\u7f6e\u9879\n\n| \u53c2\u6570 | \u7c7b\u578b | \u63cf\u8ff0 |\n|------|------|------|\n| `max_turns` | `int` | \u6700\u5927\u5bf9\u8bdd\u8f6e\u6570 |\n| `system_prompt` | `str` | \u7cfb\u7edf\u63d0\u793a\u8bcd |\n| `model` | `str` | \u4f7f\u7528\u7684\u6a21\u578b\u540d\u79f0 |\n| `temperature` | `float` | \u54cd\u5e94\u968f\u673a\u6027 (0.0-1.0) |\n| `max_tokens` | `int` | \u6700\u5927\u54cd\u5e94\u957f\u5ea6 |\n| `allowed_tools` | `List[str]` | \u5141\u8bb8\u4f7f\u7528\u7684\u5de5\u5177\u5217\u8868 |\n| `permission_mode` | `str` | \u6743\u9650\u6a21\u5f0f |\n| `cwd` | `str` | \u5de5\u4f5c\u76ee\u5f55 |\n| `extra_args` | `List[str]` | \u989d\u5916\u7684\u547d\u4ee4\u884c\u53c2\u6570 |\n\n### ClaudeResponse \u54cd\u5e94\u5bf9\u8c61\n\n| \u5c5e\u6027 | \u7c7b\u578b | \u63cf\u8ff0 |\n|------|------|------|\n| `output` | `str` | Claude \u7684\u54cd\u5e94\u5185\u5bb9 |\n| `return_code` | `int` | \u547d\u4ee4\u8fd4\u56de\u7801 (0 \u8868\u793a\u6210\u529f) |\n| `error` | `str` | \u9519\u8bef\u4fe1\u606f\uff08\u5982\u679c\u6709\uff09 |\n| `metadata` | `dict` | \u989d\u5916\u7684\u5143\u6570\u636e\u4fe1\u606f |\n\n## \u9ad8\u7ea7\u7528\u6cd5\n\n### \u6279\u91cf\u5904\u7406\u4e0e\u5e76\u53d1\u63a7\u5236\n\n```python\nimport asyncio\nfrom claude_cli import AsyncClaudeCLI\n\nasync def batch_process_with_limit():\n    claude = AsyncClaudeCLI()\n    \n    # \u51c6\u5907\u4efb\u52a1\u5217\u8868\n    tasks = [\n        {\"id\": 1, \"prompt\": \"\u89e3\u91ca Python \u7684 GIL\"},\n        {\"id\": 2, \"prompt\": \"\u4ec0\u4e48\u662f\u5143\u7c7b\uff1f\"},\n        {\"id\": 3, \"prompt\": \"\u89e3\u91ca\u63cf\u8ff0\u7b26\u534f\u8bae\"},\n        # ... \u66f4\u591a\u4efb\u52a1\n    ]\n    \n    # \u4f7f\u7528\u4fe1\u53f7\u91cf\u9650\u5236\u5e76\u53d1\u6570\n    semaphore = asyncio.Semaphore(3)  # \u6700\u591a 3 \u4e2a\u5e76\u53d1\u8bf7\u6c42\n    \n    async def process_with_limit(task):\n        async with semaphore:\n            response = await claude.query(task[\"prompt\"])\n            return {\"id\": task[\"id\"], \"result\": response.output}\n    \n    # \u5e76\u53d1\u5904\u7406\u6240\u6709\u4efb\u52a1\n    results = await asyncio.gather(*[process_with_limit(t) for t in tasks])\n    return results\n```\n\n### \u9519\u8bef\u5904\u7406\u4e0e\u91cd\u8bd5\n\n```python\nimport asyncio\nfrom claude_cli import AsyncClaudeCLI\n\nasync def query_with_retry(prompt, max_retries=3):\n    claude = AsyncClaudeCLI()\n    \n    for attempt in range(max_retries):\n        try:\n            response = await claude.query(prompt, timeout=10.0)\n            if response.return_code == 0:\n                return response\n            \n            # \u5982\u679c\u662f\u4e34\u65f6\u9519\u8bef\uff0c\u7b49\u5f85\u540e\u91cd\u8bd5\n            if attempt < max_retries - 1:\n                await asyncio.sleep(2 ** attempt)  # \u6307\u6570\u9000\u907f\n                \n        except Exception as e:\n            print(f\"\u5c1d\u8bd5 {attempt + 1} \u5931\u8d25: {e}\")\n            if attempt == max_retries - 1:\n                raise\n    \n    return None\n```\n\n### \u81ea\u5b9a\u4e49\u65e5\u5fd7\n\n```python\nimport logging\nfrom claude_cli import ClaudeCLI\n\n# \u914d\u7f6e\u65e5\u5fd7\nlogging.basicConfig(\n    level=logging.DEBUG,\n    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'\n)\n\n# \u521b\u5efa\u81ea\u5b9a\u4e49 logger\nlogger = logging.getLogger(\"my_app\")\n\n# \u4f7f\u7528\u81ea\u5b9a\u4e49 logger\nclaude = ClaudeCLI(logger=logger)\n```\n\n## \u5b9e\u9645\u5e94\u7528\u793a\u4f8b\n\n### \u4ee3\u7801\u5ba1\u67e5\u52a9\u624b\n\n```python\nfrom claude_cli import ClaudeCLI\n\nclass CodeReviewer:\n    def __init__(self):\n        self.claude = ClaudeCLI()\n    \n    def review_code(self, code: str, language: str = \"python\"):\n        prompt = f\"\"\"\u8bf7\u5ba1\u67e5\u4ee5\u4e0b {language} \u4ee3\u7801\uff1a\n        \n```{language}\n{code}\n```\n\n\u63d0\u4f9b\uff1a\n1. \u6f5c\u5728\u7684 bug \u6216\u95ee\u9898\n2. \u6027\u80fd\u4f18\u5316\u5efa\u8bae\n3. \u4ee3\u7801\u98ce\u683c\u6539\u8fdb\n4. \u5b89\u5168\u6027\u8003\u8651\n\"\"\"\n        response = self.claude.query(prompt)\n        return response.output if response.return_code == 0 else None\n\n# \u4f7f\u7528\u793a\u4f8b\nreviewer = CodeReviewer()\ncode = \"\"\"\ndef get_user(user_id):\n    return db.query(f\"SELECT * FROM users WHERE id = {user_id}\")\n\"\"\"\nreview = reviewer.review_code(code)\nprint(review)\n```\n\n### \u6587\u6863\u751f\u6210\u5668\n\n```python\nasync def generate_docs(file_path: str):\n    claude = AsyncClaudeCLI()\n    \n    with open(file_path, 'r') as f:\n        code = f.read()\n    \n    prompt = f\"\"\"\u4e3a\u4ee5\u4e0b\u4ee3\u7801\u751f\u6210\u8be6\u7ec6\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff1a\n\n{code}\n\n\u8981\u6c42\uff1a\n- \u4f7f\u7528 Google \u98ce\u683c\u7684\u6587\u6863\u5b57\u7b26\u4e32\n- \u5305\u542b\u53c2\u6570\u8bf4\u660e\u548c\u8fd4\u56de\u503c\n- \u6dfb\u52a0\u4f7f\u7528\u793a\u4f8b\n\"\"\"\n    \n    response = await claude.query(prompt)\n    return response.output\n```\n\n## \u8fd0\u884c\u793a\u4f8b\n\n\u9879\u76ee\u5305\u542b\u591a\u4e2a\u793a\u4f8b\u811a\u672c\uff1a\n\n```bash\n# \u57fa\u7840\u793a\u4f8b\npython examples/basic_usage.py\n\n# \u5f02\u6b65\u793a\u4f8b\npython examples/async_usage.py\n\n# \u9ad8\u7ea7\u7528\u6cd5\npython examples/advanced_usage.py\n\n# \u6d4b\u8bd5\u811a\u672c\npython test_basic.py\npython test_async.py\npython code_analyzer.py\n```\n\n## \u6545\u969c\u6392\u9664\n\n### \u5e38\u89c1\u95ee\u9898\n\n1. **\"Claude CLI 'claudee' not found in PATH\"**\n   - \u786e\u4fdd `claudee` \u547d\u4ee4\u5df2\u6b63\u786e\u5b89\u88c5\n   - \u68c0\u67e5 PATH \u73af\u5883\u53d8\u91cf\n   - \u5c1d\u8bd5\u4f7f\u7528\u5b8c\u6574\u8def\u5f84\u521d\u59cb\u5316\uff1a`ClaudeCLI(command=\"/full/path/to/claudee\")`\n\n2. **\u8d85\u65f6\u9519\u8bef**\n   - \u589e\u52a0 timeout \u53c2\u6570\uff1a`claude.query(prompt, timeout=30.0)`\n   - \u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\n   - \u8003\u8651\u4f7f\u7528\u5f02\u6b65\u6a21\u5f0f\u5904\u7406\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u67e5\u8be2\n\n3. **JSON \u89e3\u6790\u9519\u8bef**\n   - \u786e\u4fdd Claude \u8fd4\u56de\u7684\u662f\u6709\u6548\u7684 JSON \u683c\u5f0f\n   - \u4f7f\u7528 try-except \u5904\u7406\u89e3\u6790\u9519\u8bef\n\n### \u8c03\u8bd5\u6280\u5de7\n\n```python\n# \u542f\u7528\u8be6\u7ec6\u65e5\u5fd7\nimport logging\nlogging.basicConfig(level=logging.DEBUG)\n\n# \u67e5\u770b\u5b8c\u6574\u547d\u4ee4\nresponse = claude.query(\"test\")\nprint(response.metadata[\"command\"])  # \u663e\u793a\u5b9e\u9645\u6267\u884c\u7684\u547d\u4ee4\n```\n\n## \u9879\u76ee\u7ed3\u6784\n\n```\nclaude-cli-wrapper/\n\u251c\u2500\u2500 claude_cli/\n\u2502   \u251c\u2500\u2500 __init__.py        # \u5305\u5bfc\u51fa\n\u2502   \u251c\u2500\u2500 wrapper.py         # \u540c\u6b65\u5305\u88c5\u5668\u5b9e\u73b0\n\u2502   \u2514\u2500\u2500 async_wrapper.py   # \u5f02\u6b65\u5305\u88c5\u5668\u5b9e\u73b0\n\u251c\u2500\u2500 examples/\n\u2502   \u251c\u2500\u2500 basic_usage.py     # \u57fa\u7840\u7528\u6cd5\u793a\u4f8b\n\u2502   \u251c\u2500\u2500 async_usage.py     # \u5f02\u6b65\u64cd\u4f5c\u793a\u4f8b\n\u2502   \u2514\u2500\u2500 advanced_usage.py  # \u9ad8\u7ea7\u529f\u80fd\u793a\u4f8b\n\u251c\u2500\u2500 tests/                 # \u6d4b\u8bd5\u76ee\u5f55\n\u251c\u2500\u2500 pyproject.toml         # \u9879\u76ee\u914d\u7f6e\n\u251c\u2500\u2500 README.md             # \u672c\u6587\u6863\n\u251c\u2500\u2500 test_basic.py         # \u57fa\u7840\u6d4b\u8bd5\u811a\u672c\n\u251c\u2500\u2500 test_async.py         # \u5f02\u6b65\u6d4b\u8bd5\u811a\u672c\n\u2514\u2500\u2500 code_analyzer.py      # \u4ee3\u7801\u5206\u6790\u5668\u793a\u4f8b\n```\n\n## \u8d21\u732e\u6307\u5357\n\n\u6b22\u8fce\u8d21\u732e\uff01\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\n\n1. Fork \u9879\u76ee\n2. \u521b\u5efa\u529f\u80fd\u5206\u652f (`git checkout -b feature/amazing-feature`)\n3. \u63d0\u4ea4\u66f4\u6539 (`git commit -m 'Add amazing feature'`)\n4. \u63a8\u9001\u5230\u5206\u652f (`git push origin feature/amazing-feature`)\n5. \u521b\u5efa Pull Request\n\n### \u5f00\u53d1\u8981\u6c42\n\n- \u9075\u5faa PEP 8 \u4ee3\u7801\u98ce\u683c\n- \u6dfb\u52a0\u9002\u5f53\u7684\u7c7b\u578b\u6ce8\u89e3\n- \u7f16\u5199\u5355\u5143\u6d4b\u8bd5\n- \u66f4\u65b0\u6587\u6863\n\n## \u8bb8\u53ef\u8bc1\n\nMIT License - \u8be6\u89c1 [LICENSE](LICENSE) \u6587\u4ef6\n\n## \u4f5c\u8005\n\n- jancee (@jancee)\n\n## \u81f4\u8c22\n\n- \u611f\u8c22 Anthropic \u63d0\u4f9b Claude CLI \u5de5\u5177\n- \u611f\u8c22\u6240\u6709\u8d21\u732e\u8005\n\n---\n\n\u5982\u6709\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u8bf7\u63d0\u4ea4 [Issue](https://github.com/jancee/claude-code-python-wrapper/issues) \u6216 [Pull Request](https://github.com/jancee/claude-code-python-wrapper/pulls)\u3002\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A comprehensive Python wrapper for Claude CLI with async support",
    "version": "0.1.0",
    "project_urls": {
        "Documentation": "https://github.com/jancee/claude-code-python-wrapper/blob/main/README.md",
        "Homepage": "https://github.com/jancee/claude-code-python-wrapper",
        "Issues": "https://github.com/jancee/claude-code-python-wrapper/issues",
        "Repository": "https://github.com/jancee/claude-code-python-wrapper"
    },
    "split_keywords": [
        "claude",
        " cli",
        " wrapper",
        " ai",
        " async",
        " translation"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "d231d491120abc5e69ea8729bf9b7c985aa64e77e6c5963099258b42d1e6c94a",
                "md5": "41c3acb8f1f4c60a71a88321ae5fed93",
                "sha256": "a2f2348b3fe1e723f4d4631cddf7d97ff080238c3a9ec466f9348b6247031d69"
            },
            "downloads": -1,
            "filename": "claude_code_python_wrapper-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "41c3acb8f1f4c60a71a88321ae5fed93",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 10372,
            "upload_time": "2025-07-17T13:46:00",
            "upload_time_iso_8601": "2025-07-17T13:46:00.556927Z",
            "url": "https://files.pythonhosted.org/packages/d2/31/d491120abc5e69ea8729bf9b7c985aa64e77e6c5963099258b42d1e6c94a/claude_code_python_wrapper-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "085ad8821c22163e49dde0a06e5182e5967de0270c6b96dfcfa593fbc7381c8f",
                "md5": "d667b936e8636262e3931999335ed30b",
                "sha256": "ec5d5e3d756fa2b7cef96c15de9aa54049c6508534312d2642fdcf4170f9b675"
            },
            "downloads": -1,
            "filename": "claude_code_python_wrapper-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d667b936e8636262e3931999335ed30b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 29911,
            "upload_time": "2025-07-17T13:46:01",
            "upload_time_iso_8601": "2025-07-17T13:46:01.816978Z",
            "url": "https://files.pythonhosted.org/packages/08/5a/d8821c22163e49dde0a06e5182e5967de0270c6b96dfcfa593fbc7381c8f/claude_code_python_wrapper-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-17 13:46:01",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jancee",
    "github_project": "claude-code-python-wrapper",
    "github_not_found": true,
    "lcname": "claude-code-python-wrapper"
}
        
Elapsed time: 2.81458s