简体   繁体   English

Visual Studio Code 不建议相对导入

[英]Visual Studio Code is not suggesting relative imports

For some reason, if I have a variable inside my project, Vscode stopped suggesting imports when i press Ctrl + Space .出于某种原因,如果我的项目中有一个变量,当我按下Ctrl + Space时,Vscode 会停止建议导入。

For example, I have a class in types.py :例如,我在 types.py 中有一个types.py

class StepStatus(ExtendedEnum):
    RUNNING = "RUNNING"
    ERROR = "ERROR"
    OK = "OK"

At the same level, another file called runner.py , but if try to auto import this variable it raises no suggestions :在同一级别,另一个名为runner.py的文件,但如果尝试自动导入此变量,它不会提出no suggestions 在此处输入图像描述

But if I manually import the StepStatus variable it exists:但是,如果我手动导入StepStatus变量,它就存在:

在此处输入图像描述

There is my vscode settings.json content:还有我的vscode settings.json 。json内容:

{
    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        },
    },
    "isort.args": [
        "--profile",
        "black"
    ],
    "python.analysis.extraPaths": [
        "${workspaceFolder}"
    ],
    "workbench.iconTheme": "material-icon-theme",
    "workbench.colorTheme": "Material Theme High Contrast",
    "window.zoomLevel": 1,
    "terminal.integrated.inheritEnv": false
}

I already tried to reinstall Vscode, reinstall Pylance extension, delete cache but nothing works.我已经尝试重新安装 Vscode,重新安装 Pylance 扩展,删除缓存但没有任何效果。 The auto import suggestions only works for the default libraries such as json, typing, datetime...自动导入建议仅适用于默认库,例如 json、打字、日期时间...

If there is no StepStatus class in your script, how does IntelliSense prompt?如果你的脚本中没有StepStatus class,IntelliSense如何提示?

So I guess you have the following code under your types.py script所以我猜你的types.py脚本下有以下代码

class StepStatus(Enum):
    EMPTY = "empty"
    PARTLY_OK = "partly_ok"
    OK = "ok"
    ERROR = "error"

Then intellisense will be provided when you type StepStatus in the runner.py script in the same directory:然后,当您在同一目录中的runner.py脚本中键入StepStatus时,将提供智能感知:

在此处输入图像描述

Enter or select, it will be imported automatically输入或select,会自动导入

在此处输入图像描述

Type .键入. and smart suggestions also work和聪明的建议也有效

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM