添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

VSCode:如何为Python配置 "组织导入"(isort)。

5 人关注

Mirror question to

  • " How to config VSCode's Organize Imports order? " which refers to a .js project
  • 我想配置VSCode如何调用 isort ,所以我可以在.py文件中调用 Organize imports 时进行自定义。

    特别是,VSCode已经开始在两个isort-section之间删除一个空行,不知道为什么。

    from django...
    from myproject... # removing blanck line between 2 sections
        
    3 个评论
    看来这可能是有帮助的。 medium.com/@cereblanco/... 你可以通过改变 "python.sortImports.args" 进行定制。我正在使用的另一个替代方案是 pre-commit isort
    当尝试这些时,出现了一个弹出窗口。"使用了以下被废弃的CLI标志,但被忽略了:-rc!"
    我认为递归选项已被废弃,因为它是自动完成的。 github.com/PyCQA/isort/issues/1263
    python
    visual-studio-code
    isort
    Manu Artero
    Manu Artero
    发布于 2021-04-12
    3 个回答
    Jill Cheng
    Jill Cheng
    发布于 2022-03-23
    已采纳
    0 人赞同

    在VS Code中,"Python "扩展为我们提供了以下设置,它可以将同一模块的特定导入合并为一个导入语句,并按字母顺序组织导入语句。(在 "settings.json "文件中)

    "python.sortImports.args": ["-rc", "--atomic"],
    

    关于在VS代码中使用 "Sort Imports",请参考此文件。Sort Imports in VS Code.

    你知道哪些标志可以用于 "预期 "顺序(不会导致 pylint-wrong-import 错误)吗?
    @Manu -很抱歉我不知道这个问题,但你可以创建一个新的主题,详细描述这个问题,以获得更好的答案。
    xshapira
    xshapira
    发布于 2022-03-23
    0 人赞同

    你可以使 isort black 兼容,并在保存文档时享受格式化的代码。以下是实现这一目的的两种方法。

  • You can configure Black and Isort ’s settings on pyproject.toml . Inside your root’s project folder, create/update your pyproject.toml file:
  • [tool.isort]
    multi_line_output = 3
    include_trailing_comma = true
    force_grid_wrap = 0
    line_length = 88
    profile = "black"
    
  • Edit settings.json and configure Black and Isort.
  • "[python]": { "editor.codeActionsOnSave": { "source.organizeImports": true "python.formatting.provider": "black", "python.sortImports.args": ["--profile", "black"],