Py学习  »  Python

12月更新 | Visual Studio Code Python

玩转VSCode • 2 年前 • 369 次点击  

点击上方蓝字

关注我们

(本文阅读时间:10分钟)


我们很高兴地宣布,2022年12月发布的适用于 Visual Studio Code Python 和 Jupyter 扩展现已推出!

此版本包括以下改进:

  • Pylance 自动缩进 

  • 预览:浏览器中运行与调试 Python

  • 社区提供新扩展:Ruff

如果您有兴趣,可以在我们的更新日志中查看 Python、Jupyter 和 Pylance 扩展的完整改进列表。

  • Python 1:

    https://marketplace.visualstudio.com/items?itemName=ms-python.python

  • Jupyter 1:

    https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter

  • Python 2:

    https://github.com/microsoft/vscode-python/releases

  • Jupyter 2:

    https://github.com/microsoft/vscode-jupyter/blob/main/CHANGELOG.md

  • Pylance:

    https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md




Pylance 自动缩进 


VS Code 的 Pylance 扩展通过更深入地理解 Python 改进自动缩进功能!此功能使您可以更多地关注代码的逻辑,而不是格式化。在 VS Code Insiders 中使用 Python 扩展的预发布版本来尝试这个功能!您可以通过验证用户 settings.json 文件中是否添加以下设置来判断它是否启用(View > Command Palette and run “Preferences: Open User Settings (JSON)”):

"[python]": { 
"editor.formatOnType": true,
},

在接下来的几个月里,我们将默认启用此设置。如果您有任何问题或反馈,请在 Pylance GitHub 存储库中告诉我们。 

  • VS Code Insiders
    https://code.visualstudio.com/insiders/?ocid=AID3052907
  • Pylance GitHub 存储库
    https://github.com/microsoft/pylance-release



预览:浏览器中的 Python


您现在可以尝试在浏览器中的 vscode.dev 中运行和调试 Python 代码! 

有一个新的 VS 代码扩展,允许您尝试使用 WebAssembly 作为执行引擎在 Web 中运行 Python 代码。请注意,此扩展用于实验和研究目的,以了解使用 WebAssembly 在 Web 中运行 Python 代码,不应将其用于生产 Python 开发,因为这存在重大限制。 

请按照以下步骤试用此预览: 

  • 打开基于 Chrome 的浏览器(例如 Microsoft Edge 或 Google Chrome) 

  • 使用 URL 模板URL https://insiders.vscode.dev/github/{username}/{repo}?vscode-coi= 导航到 GitHub 存储库;更换 {username}{repo} 满足您的需求。如果需要,可以使用https://insiders.vscode.dev/github/dbaeumer/python-sample?vscode-coi= 。确保查询参数 ?vscode-coi= 附加到 URL

  • 安装扩展的最新预发布版本 Experimental - Python for the Web

  • 打开一个 Python 文件 

  • 使用编辑器的运行菜单执行文件 

  • 使用相同的运行菜单调试 Python 文件 

  • 创建启动配置(例如) 

{    "type": "python-web-wasm",    "name""Debug Active Python File"   "request": "launch",    "program": "${file}",    "stopOnEntry": true,    "console": "integratedTerminal" }


  • 使用启动配置开始调试

  • vscode.dev
    https://devblogs.microsoft.com/python/python-in-visual-studio-code-december-2022-release/vscode.dev?ocid=AID3052907
  • WebAssembly
    https://webassembly.org/
  • Experimental - Python for the Web 
    https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-python-web-wasm


社区亮点:Ruff


@charliermarsh,ruff linter 的创建者,使用 Python 工具扩展模板创建了一个附带的 Ruff 扩展。这个 VS Code 扩展提供了对 Ruff linter 的支持,目前附带 ruff==0.0.138 。在 Visual Studio Code 中安装后,当您打开或编辑 Python 文件时,Ruff 将在您的代码上运行。支持包括替换 flake8 使用的 linting、使用 Organize Importsisort 兼容导入排序、相当于 Ruff 的 --fix 支持的自动修复违规的“快速修复”操作,以及用于自动修复违规的 Fix all 命令。 

此外,您可以通过在 settings.json 中启用 source.fixAll 操作,将 Ruff 配置为在保存时自动修复违规行为: 

{    "editor.codeActionsOnSave": {        "source.fixAll": true    } }

就像 Charlie 一样,您可以使用扩展模板来创建您自己喜欢的 linter 或格式化程序的 VS 代码扩展,并通过更改核心 Python 代码来实现!有关该模板的更多信息,请参考我们的2022年8月版本并尝试使用 Ruff 扩展! 

  • @charliermarsh
    https://github.com/charliermarsh
  • ruff linter
    https://pypi.org/project/ruff/
  • Python 工具扩展模板
    https://github.com/microsoft/vscode-python-tools-extension-template
  • Ruff 扩展
    https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff
  • 2022 年 8 月版本

    https://devblogs.microsoft.com/python/python-in-visual-studio-code-august-2022-release/#python-tools-extension-template?ocid=AID3052907




其他变化和增强

我们还添加了一些小的增强功能并修复了用户请求的问题,这些功能应该可以改善您在 Visual Studio Code 中使用 Python 和 Jupyter Notebooks 的体验。一些显著的变化包括:

  • Jupyter 默认支持粘贴图像(vscode-jupyter#11987) 

  • 支持为 Jupyter 调试禁用“justMyCode” (vscode-jupyter#8146) 

  • vscode-jupyter#11987
    https://github.com/microsoft/vscode-jupyter/issues/11987
  • vscode-jupyter#8146:
    https://github.com/microsoft/vscode-jupyter/issues/8146

通过从 Marketplace 更新或下载 Python 扩展和 Jupyter 扩展来尝试这些新改进,或者直接从 Visual Studio Code 中的扩展视图安装它们(Ctrl + Shift + X 或 ⌘ + ⇧ + X)。您可以在文档中了解有关 Visual Studio Code Python 的更多信息。如果您遇到任何问题或有建议,请在 Python VS Code GitHub 页面上提出问题。 
  • Python 扩展:
    https://marketplace.visualstudio.com/items?itemName=ms-python.python
  • Jupyter 扩展:
    https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
  • 文档:
    https://code.visualstudio.com/docs/python/python-tutorial?ocid=AID3052907
  • GitHub:
    https://github.com/Microsoft/vscode-python
  • 提出问题:

    https://github.com/microsoft/vscode-python/issues/new/choose


欢迎前往 Python in Visual Studio Code 官方站点,了解更多。


点击「阅读原文」前往官方文档~

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/151283
 
369 次点击