跳到主要内容

python

💡 本指南灵感源自 Jack Fields 关于配置 VS Code 进行 Python 开发的优秀文章。欲了解更多细节,请查阅他的原文。(注:此处需替换为实际文章链接)

前提条件

开始前请确保已安装:

  • Python(推荐 3.8 或更高版本)
  • Git(用于版本控制)
  • 已安装并更新至最新版本的 Cursor

必备扩展

Python 核心支持

以下扩展将赋予 Cursor 完整的 Python 开发功能,包括语法高亮、代码检查、调试和单元测试:

PythonCore language support from Microsoft (微软提供的 Python 核心语言支持) PylanceFast Python language server (快速的 Python 语言服务器) Python DebuggerEnhanced debugging capabilities (增强的调试功能) Python Test ExplorerVisual testing interface (可视化测试界面)

代码质量工具

Python Docstring Generator自动生成文档 Python Path管理 Python 路径 Python Environment Manager虚拟环境管理 Python SnippetsPython 代码片段

高级 Python 工具

虽然上述扩展之前是 Cursor 中 Python 开发最受欢迎的扩展,但我们也新增了一些其他扩展,助您充分发挥 Python 开发的潜力。

uv - Python 环境管理器

uv 是一款现代化的 Python 包管理器,除了可以替代 pip 作为默认包管理器外,还能用于创建和管理虚拟环境。

要安装 uv,请在终端中运行以下命令:

code]:pr-[3rem] [&_pre>code>span.line-highlight]:min-w-[calc(100%+3rem)] [&_pre>code>span.line-diff]:min-w-[calc(100%+3rem)] rounded-2xl bg-white overflow-x-auto scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-black/15 hover:scrollbar-thumb-black/20 active:scrollbar-thumb-black/20 dark:scrollbar-thumb-white/20 dark:hover:scrollbar-thumb-white/25 dark:active:scrollbar-thumb-white/25" component-part="code-block-root" style="font-variant-ligatures: none; height: auto;">

pip install uv

Ruff - Python 代码检查与格式化工具

Ruff 是一款现代化的 Python 代码检查与格式化工具,可用于检测编程错误、强化编码规范并提供重构建议。该工具可与 Black 代码格式化工具配合使用。

在终端中运行以下命令安装 Ruff:

pip install ruff

Cursor 配置

1. Python 解释器

在 Cursor 中配置 Python 解释器:

  1. 打开命令面板(Cmd/Ctrl + Shift + P)
  2. 搜索 "Python: Select Interpreter"
  3. 选择你的 Python 解释器(如果使用了虚拟环境,请选择对应的虚拟环境)

2. 代码格式化

配置 Black 实现自动代码格式化:

💡 Black 是一款代码格式化工具,能够自动将代码格式化为统一风格。它无需任何配置,且已被 Python 社区广泛采用。

在终端中运行以下命令安装 Black:

code]:pr-[3rem] [&_pre>code>span.line-highlight]:min-w-[calc(100%+3rem)] [&_pre>code>span.line-diff]:min-w-[calc(100%+3rem)] rounded-2xl bg-white overflow-x-auto scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-black/15 hover:scrollbar-thumb-black/20 active:scrollbar-thumb-black/20 dark:scrollbar-thumb-white/20 dark:hover:scrollbar-thumb-white/25 dark:active:scrollbar-thumb-white/25" component-part="code-block-root" style="font-variant-ligatures: none; height: auto;">

pip install black

通过向 settings.json 文件添加以下配置,设置 Cursor 使用 Black 进行代码格式化:

code]:pr-[3rem] [&_pre>code>span.line-highlight]:min-w-[calc(100%+3rem)] [&_pre>code>span.line-diff]:min-w-[calc(100%+3rem)] rounded-2xl bg-white overflow-x-auto scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-black/15 hover:scrollbar-thumb-black/20 active:scrollbar-thumb-black/20 dark:scrollbar-thumb-white/20 dark:hover:scrollbar-thumb-white/25 dark:active:scrollbar-thumb-white/25" component-part="code-block-root" style="font-variant-ligatures: none; height: auto;">

{
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"python.formatting.blackArgs": [
"--line-length",
"88"
]
}

3. 代码检查

我们可以使用 PyLint 来检查编程错误、强制执行代码规范,并提供代码重构建议。

要安装 PyLint,请在终端中运行以下命令:

code]:pr-[3rem] [&_pre>code>span.line-highlight]:min-w-[calc(100%+3rem)] [&_pre>code>span.line-diff]:min-w-[calc(100%+3rem)] rounded-2xl bg-white overflow-x-auto scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-black/15 hover:scrollbar-thumb-black/20 active:scrollbar-thumb-black/20 dark:scrollbar-thumb-white/20 dark:hover:scrollbar-thumb-white/25 dark:active:scrollbar-thumb-white/25" component-part="code-block-root" style="font-variant-ligatures: none; height: auto;">

pip install pylint

code]:pr-[3rem] [&_pre>code>span.line-highlight]:min-w-[calc(100%+3rem)] [&_pre>code>span.line-diff]:min-w-[calc(100%+3rem)] rounded-2xl bg-white overflow-x-auto scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-black/15 hover:scrollbar-thumb-black/20 active:scrollbar-thumb-black/20 dark:scrollbar-thumb-white/20 dark:hover:scrollbar-thumb-white/25 dark:active:scrollbar-thumb-white/25" component-part="code-block-root" style="font-variant-ligatures: none; height: auto;">

{
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.lintOnSave": true
}

4. 类型检查

除了代码规范检查之外,我们还可以使用 MyPy 进行类型错误检查。

在终端中运行以下命令安装 MyPy:

code]:pr-[3rem] [&_pre>code>span.line-highlight]:min-w-[calc(100%+3rem)] [&_pre>code>span.line-diff]:min-w-[calc(100%+3rem)] rounded-2xl bg-white overflow-x-auto scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-black/15 hover:scrollbar-thumb-black/20 active:scrollbar-thumb-black/20 dark:scrollbar-thumb-white/20 dark:hover:scrollbar-thumb-white/25 dark:active:scrollbar-thumb-white/25" component-part="code-block-root" style="font-variant-ligatures: none; height: auto;">

pip install mypy

通过以下 JSON 配置启用 MyPy 检查:

code]:pr-[3rem] [&_pre>code>span.line-highlight]:min-w-[calc(100%+3rem)] [&_pre>code>span.line-diff]:min-w-[calc(100%+3rem)] rounded-2xl bg-white overflow-x-auto scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-black/15 hover:scrollbar-thumb-black/20 active:scrollbar-thumb-black/20 dark:scrollbar-thumb-white/20 dark:hover:scrollbar-thumb-white/25 dark:active:scrollbar-thumb-white/25" component-part="code-block-root" style="font-variant-ligatures: none; height: auto;">

{
"python.linting.mypyEnabled": true
}

调试

Cursor 为 Python 提供了强大的调试功能:

  1. 通过点击行号旁的侧边栏设置断点
  2. 使用调试面板(Cmd/Ctrl + Shift + D)
  3. 配置 launch.json 文件实现自定义调试配置

推荐功能

Tab 自动补全智能化的代码建议,深度理解您的编码行为聊天通过自然对话探索和理解代码智能体借助 AI 协助处理复杂开发任务上下文从第三方系统获取相关上下文自动导入编码时自动导入所需模块AI 审查Cursor 持续通过 AI 进行代码质量审查

框架支持

Cursor 与主流 Python 框架实现无缝协作:

  • Web 框架:Django、Flask、FastAPI
  • 数据科学:Jupyter、NumPy、Pandas
  • 机器学习:TensorFlow、PyTorch、scikit-learn
  • 测试工具:pytest、unittest
  • API 工具:requests、aiohttp
  • 数据库:SQLAlchemy、psycopg2

(注:根据翻译规则,所有技术框架名称均保留原始英文形式,分类标题采用符合中文技术文档习惯的表达方式,标点符号使用全角中文符号并保持中英文间合理间距)