【问题标题】:Prevent VSCode from reorganizing a specific list of imports防止 VSCode 重新组织特定的导入列表
【发布时间】:2021-11-25 19:30:52
【问题描述】:

我目前有一个如下所示的 python 文件:

import os
import sys

PROJECT_FOLDER = os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))
os.chdir(PROJECT_FOLDER)
sys.path.insert(0, PROJECT_FOLDER)
from settings import datagenerator as settings

由于项目中设置和模块的组织方式,我需要在导入各种文件之前设置一些常用的工作目录。

由于我非常,我喜欢在 VScode 中使用“组织导入”。不幸的是,它将from settings import datagenerator as settings行移动到设置当前路径的块顶部。

尽管这是意料之中的。我想知道是否有办法告诉 VSCode 将这些行放在一起。

【问题讨论】:

    标签: python visual-studio-code


    【解决方案1】:

    扩展使用isort 来管理导入。因此,如果 isort 提供了一种将导入标记为不移动的机制,那么扩展也将支持它。否则恐怕没有什么可以做的。

    【讨论】:

      【解决方案2】:

      正如this 的回答,解释说,你可以避免这样的排序:

      app = Flask(__name__)
      from . import views  # isort:skip
      i.e. Add the # isort:skip comment to the import you don't want to jump to the top of the file.
      

      来源:https://github.com/timothycrosley/isort#skip-processing-of-imports-outside-of-configuration

      【讨论】:

        猜你喜欢
        • 2020-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-09
        • 2021-04-22
        • 2020-07-09
        • 1970-01-01
        • 2023-01-09
        相关资源
        最近更新 更多