返回资讯中心
外部精选
大数据
#数据科学#机器学习

年末值得尝试的12个Python库

来源标题:12 Python Libraries You Need to Try by Year End

一文盘点年底前值得尝试的12个Python库,覆盖数据、AI智能体和开发者工具三大方向。其中包括Pydantic团队推出的Pydantic AI,主打类型化、可校验的输出,目前约有2.1万GitHub星标和540万月下载量;以及OpenAI Agents SDK等工具。

KDnuggetsShittu Olumide2026/12/5 · 今天20 分钟阅读

以下正文由 AI 翻译自 KDnuggets(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

Pydantic AI 出自 Pydantic 团队之手,专注于带类型、经过校验的输出。你用 Pydantic 模型描述答案的形状,框架会让模型的回复符合这个形状。根据 BetterClaw 十一月的统计数据,它大约有 21K stars 和 540 万月下载量。它只是一个库,没有配套的托管部署产品,有些团队会喜欢这一点。 from pydantic import BaseModel from pydantic_ai import Agent from pydantic_ai.models.test import TestModel class CityInfo(BaseModel): city: str country: str agent = Agent(TestModel(), output_type=CityInfo) # TestModel needs no API key result = agent.run_sync("Where is the Eiffel Tower?") print(result.output) print(type(result.output).name) CityInfo 是答案必须遵循的 schema,output_type 告诉 agent 返回该类型。TestModel 是内置的假模型,会用占位值填充 schema,因此你可以离线测试整条链路。输出是真正的 CityInfo 对象,而不是松散的文本。要使用真实模型,把 TestModel() 换成 Pydantic AI 文档中的模型字符串,并设置对应提供商的 API key。

OpenAI Agents SDK 是一个小巧的 Python 工具包,包含 agent、工具、agent 之间的交接(handoff)、guardrail 和 session。它有超过 30,500 stars,是上面三个较新框架中月下载量最高的。它目前仍是 0.x 版本,因此不同版本之间 API 可能发生变化。 from agents import Agent, Runner, function_tool def word_count(text: str) -> int: """Count the words in a piece of text.""" return len(text.split()) agent = Agent( name="Editor", instructions="Use the word_count tool when asked about length.", tools=[word_count], ) if name == "main":

Needs OPENAI_API_KEY in your environment

result = Runner.run_sync(agent, "How many words are in 'to be or not to be'?") print(result.final_output) @function_tool 把一个普通函数变成工具,利用类型提示和 docstring 向模型描述它。Agent 把名称、instructions 和工具打包在一起。Runner.run_sync 运行整个循环:模型决定调用 word_count,SDK 执行它,最终答案通过 final_output 返回。

Google 的 ADK 是代码优先的,专为 Gemini 和 Google Cloud 打造,提供 Python、TypeScript、Java 和 Go 版本。Python 仓库大约有 22K stars 和 1,010 万月下载量,ADK 2.0 增加了基于图的工作流,适用于需要显式控制流的 agent。它可以部署到 Cloud Run 或 Vertex AI Agent Engine。

clock_agent/agent.py

from google.adk.agents import Agent def get_time(city: str) -> dict: """Return a fixed time for a city (stand-in for a real lookup).""" return {"city": city, "time": "10:30"} root_agent = Agent( name="clock_agent", model="gemini-2.5-flash", # use any Gemini model your key can access description="Answers questions about the time in a city.", instruction="Use the get_time tool to answer time questions.", tools=[get_time], ) ADK 同样利用函数签名和 docstring,把普通 Python 函数读作工具。agent 必须命名为 root_agent,命令行界面才能找到它。把这个文件放在 clock_agent/agent.py,添加一个包含 from . import agent 的 init.py,设置 GOOGLE_API_KEY,然后在父目录运行 adk run clock_agent 即可与它对话。

FastMCP 是用于构建 MCP 服务器的 Python 框架,也就是你的 agent 所调用的那些工具。它也是这一组里最新的发布。FastMCP 4 已稳定,针对新版 MCP 构建,维护者表示大多数 FastMCP 3 应用无需改代码即可升级。有一个破坏性变更需要注意:服务端发起的 sampling 已被移除。

关于采用情况,项目自己的 PyPI 页面称它每天被下载一百万次,为 70% 的 MCP 服务器提供支持——这是维护者的说法,并非独立数据。一篇外部报道称它在七月有 26,000 GitHub stars。 import asyncio from fastmcp import FastMCP, Client mcp = FastMCP("Demo") def add(a: int, b: int) -> int: """Add two numbers.""" return a + b async def main():

Passing the server object connects in memory, no network or subprocess

async with Client(mcp) as client: result = await client.call_tool("add", {"a": 2, "b": 3}) print(result.data) if name == "main": asyncio.run(main())

今年工具领域的大事件是 Astral。OpenAI 于 3 月 19 日宣布将收购 Astral,即 uv、Ruff 和 ty 背后的公司。开源许可证是宽松型的(MIT 和 Apache 2.0),创始人 Charlie Marsh 在 Talk Python 播客上表示,交易前后的发布频率大致保持不变。底下还有第二条主线:Rust。据估计,现在 25% 到 33% 的原生 PyPI 扩展是用 Rust 写的,而 uv、Ruff 和 ty 都是这一转变的一部分。

uv 用一个工具取代 pip、virtualenv 和 pip-tools。在 2025 年 Python Software Foundation 与 JetBrains 的调查中,uv 已有 11% 的采用率;一篇基于 pypistats 的报道统计,截至 10 月 8 日的 30 天内它约有 1.28 亿次下载。 uv init demo # create a project with pyproject.toml cd demo uv add polars # install a package and record it uv run main.py # run inside the project's environment uv init 创建项目文件。uv add 把 Polars 安装到受管理的虚拟环境中,并写入 pyproject.toml 和 lockfile。uv run 在该环境中执行你的脚本,无需你手动激活任何东西,因此同样的命令在任何机器上都能用。

Ruff 集 linter 和 formatter 于一身,是 Astral 三个项目中下载量最高的工具——根据同一篇报道,截至 10 月 8 日的 30 天内约有 3.04 亿次下载。它并不新,但已成为新项目的默认选择。拿这个混乱的文件为例,保存为 messy.py: import os, sys def f(x): unused = 1 return x 运行: ruff check messy.py # list problems ruff check --fix messy.py # apply the safe fixes ruff format messy.py # apply consistent formatting

(原文较长,此处译出前 6000 字,完整内容请点「打开原文」。)

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →