英文:
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代码格式化程序,比如
isort
或yapf
。您可以在终端中运行命令pip install isort
或pip install yapf
来完成这一步。 - 在VSCode中,点击左下角的齿轮图标并选择“设置”以打开设置。
- 在搜索栏中搜索“Python › 格式化:提供者”。
- 选择“在settings.json中编辑”以编辑settings.json文件。
- 根据您安装的代码格式化程序,将以下行添加到文件中:
- 对于isort:
"python.formatting.provider": "isort"
- 对于yapf:
"python.formatting.provider": "yapf"
- 对于isort:
- 保存文件并关闭设置。
英文:
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
oryapf
. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论