Search code examples
pythonvisual-studio-codeimportpylintpylance

Why Vscode is showing an "unable to import" error


I have been looking all over the internet and spent hours trying to fix this.

Summary of the issue:

  1. When importing a class/functions from a local module, I am getting an Unable to import 'x' pylint (import-error) (image #1)
  2. Vscode is not suggesting a "quick-fix - auto import" to import an already installed library (pyngrok in this case) (image #2). Error #1 Error #2

What I tried:

  • Created a virtual env
  • Selected the virtual env as Python interpreter in Vscode
  • Installed Python-related extension: IntelliSense (Pylance)
  • Installed pylint (pip install pylint) and selected it as a linter through Vs code
  • Completely uninstall/reinstall Vscode (including caches and preferences)
  • Reloaded the window (cmd+shift+p -> Developer: Reload window)
  • Added "env": {"PYTHONPATH": "${workspaceRoot}"} to launch.json file
  • Added "python.linting.enabled": true and "python.analysis.autoImportCompletions": true to settings.json file
  • Went through the Troubleshooting linting

My project structure:

my-app/
├─ venv/
├─ errors/
│  ├─ __init__.py
│  ├─ interface
├─ _app.py
├─ __init__.py

Notes:

  • In the module I am importing from, I do have an __init__.py file
  • When running the code, it works just fine, so the import-error error is not an actual error
  • I don't want to solve it by just ignoring the import-error in the settings

My system

  • MacOS 12.5.1
  • System's Python version: 3.8
  • Project's Python virtual environment version: 3.8.
  • Vscode Version: 1.71.0

Solution

  • I was able to solve the first issue by deleting the __init__.py file from the root folder of my project.

    Regarding the second issue, as MingJie-MSFT commented - it is an issue from 2019.

    That being said - I was able to (partially) solve it, by installing the Sourcery extension for vscode.