如何防止VSCode在语句间重新排列Python导入?

huangapple go评论77阅读模式
英文:

How to prevent VSCode from reordering python imports across statements?

问题

这是将Gtk3导入Python的正确方式

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject

当我在VSCode中保存这样的代码,并使用"editor.formatOnSave": true选项时,它会被重新排序为:

from gi.repository import Gtk, Gdk
import gi
gi.require_version('Gtk', '3.0')

这会导致在我有机会指定我正在使用的版本之前加载Gtk,至少会显示以下警告:

PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '4.0') before import to ensure that the right version gets loaded.

或者更糟糕的情况下,会抛出异常:

ValueError: Namespace Gtk is already loaded with version 4.0

我喜欢VSCode的代码格式化功能,但我不希望它重新排序我的导入语句,特别是跨语句(因为Python的导入具有副作用)。如何正确使用VSCode的Python代码格式化工具与Gtk一起使用呢?

英文:

This is the correct way to import Gtk3 into python:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject

When I save such code in VSCode with "editor.formatOnSave": true, it gets reordered to:

from gi.repository import Gtk, Gdk
import gi
gi.require_version('Gtk', '3.0')

which makes Gtk to be loaded before I have the chance to specify the version I am using, which at very least leads to the following warning being displayed:

PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '4.0') before import to ensure that the right version gets loaded.

or worse, get me an exception like:

ValueError: Namespace Gtk is already loaded with version 4.0

Now, I like VSCode code formatting, but I don't want it to reorder my imports, specially not across statements (being that imports in python have side effects). How to properly use VSCode's Python code formatter with Gtk?

答案1

得分: 2

为了防止VSCode重新排列Python导入语句,您可以配置编辑器使用特定的Python代码格式化程序,以保持导入的顺序与原始代码中一致。以下是如何操作:

  • 如果尚未安装,安装VSCode的Python扩展。
  • 安装一个保持导入顺序的Python代码格式化程序,比如isortyapf。您可以在终端中运行命令pip install isortpip install yapf来完成这一步。
  • 在VSCode中,点击左下角的齿轮图标并选择“设置”以打开设置。
  • 在搜索栏中搜索“Python › 格式化:提供者”。
  • 选择“在settings.json中编辑”以编辑settings.json文件。
  • 根据您安装的代码格式化程序,将以下行添加到文件中:
    • 对于isort: "python.formatting.provider": "isort"
    • 对于yapf: "python.formatting.provider": "yapf"
  • 保存文件并关闭设置。
英文:

To prevent VSCode from reordering Python imports across statements, you can configure the editor to use a specific Python code formatter that maintains the order of the imports as they are in the original code. Here's how you can do it:

  • Install the Python extension for VSCode if you haven't already done so.
  • Install a Python code formatter that maintains the order of imports, such as isort or yapf. You can do this by running the command pip install isort or pip install yapf in your terminal.
  • In VSCode, open the settings by clicking on the gear icon in the bottom left corner and selecting "Settings".
  • Search for "Python › Formatting: Provider" in the search bar.
  • Select "Edit in settings.json" to edit the settings.json file.
  • Add the following line to the file, depending on which code formatter you installed:
  • For isort: "python.formatting.provider": "isort"
  • For yapf: "python.formatting.provider": "yapf"
  • Save the file and close the settings.

huangapple
  • 本文由 发表于 2023年2月19日 01:52:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定