Symbols defined in private modules and re-exported in public modules are auto-imported from private modules
Description
For a local project, when a symbol is defined in a private module (prefixed with _) and re-exported in a public module, Pylance's auto-import feature suggests importing from the private module instead of the public interface.
Reproduction Steps
Here's a minimal project structure to reproduce the issue:
my_package/
├── __init__.py
└── _private.py
With the following content:
my_package/_private.py
class MyClass:
def hello(self):
return "Hello from MyClass"
my_package/__init__.py
from .public import MyClass
__all__ = ["MyClass"]
Expected Behavior
When typing MyClass in a new file outside the package, Pylance should suggest:
from my_package import MyClass
Actual Behavior
Instead, Pylance suggests:
from my_package._private import MyClass
Environment
- VS Code version: 1.97
- Pylance version: 2025.2.1
I have also seen this.
Import suggestions should recommend the shortest public import path available before suggesting importing from private modules. For this to work people will need to explicitly re-export symbols by including it in __all__, otherwise it's undecidable if an import is a re-export or a use.
Any chance that this gets fixed?
Take a look at https://github.com/microsoft/pylance-release/blob/main/docs/settings/python_analysis_includeAliasesFromUserFiles.md