[Python] Make Pylance suggest all imports for my code when working in a separate directory

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    With VSCode Pylance, many import suggestions are missing when working under these conditions:

    • Package installed in editable mode
    • I'm working in a separate directory
    • Original package does not contain top-level __init__.py

    That is, suppose that I clone a python github repository and set it up for development in editable mode:

    cd C:/work
    git clone https://github.com/theskumar/python-dotenv
    pip install -e .


    When working in the same directory as the editable package (that is, C:/work/python-dotenv), VSCode/Pylance will suggest imports for everything.

    x = get_cli_string()
    x = with_warn_for_invalid_lines()


    Then, I navigate to another folder, and try to import something:

    mkdir C:/work/scripts
    cd C:/work/scripts


    x = get_cli_string()
    x = with_warn_for_invalid_lines()


    Pylance only finds the top import. It seems that by default, Pylance only locates members exported by __all__.

    I tried setting Python>Analysis>Auto Import Completions, Python>Analysis: Package Index Depths, and Python>Analysis:Extra Paths. Looking at the options and setting Python>Analysis: Package Index Depths was initially fruitful:

    "python.analysis.packageIndexDepths": [
    {
    "name": "dotenv",
    "depth": 4,
    "includeAllSymbols": true,
    }
    ]


    However, on further inspection, this solution only worked if a top-level __init__.py file was present. However, PEP 420 means many packages cannot contain a top-level __init__.py. If we delete the top-level __init__.py, then no imports are suggested.

    How can I instruct Pylance to find imports in the entire package, as if I was working in the same directory as the package itself?

    Expected behavior: see here

    Continue reading...

Compartilhe esta Página