pythonvisual-studio-codeimportpython-importpython-module

Vs-code unable to import modules modules load


I'm opening the same python project in pycharm and everything is working well opening same folder on vs code with same interpreter I'm getting error 'Unable to import' on local modules in the project what do i miss

Example tree

MyFile.py
│
├── csv (standard library)
├── io (standard library)
├── re (standard library)
│
|--  __init__.py
|
├── myproject.myfolder.mymodule
│   ├── methodA
│   ├── methodB
|   └-- __init__.py

MyFile.py

import csv
import io
import re

from myproject.myfolder.mymodule import methodA, methodB

in pycharm it working well, in Vs-code getting unable to import exception. Note - i tried to create workspace and venv, not working either

Thanks in advance


Solution

  • The problem is with PYTHONPATH. Seems like in Pycharm it configured properly, but in VSCode it isn't. You could configure PYTHONPATH manually:

    export PYTHONPATH="${PYTHONPATH}:/path/to/your/myproject.myfolder.mymodule"
    

    Or set it in .vscode/settings.json (create this folder and file if not created):

    {
      "python.autoComplete.extraPaths": [
        "./",  // Path to root of project
        "/path/to/your/myproject.myfolder.mymodule
      ],
      "python.analysis.extraPaths": [
        "./",
        "/path/to/your/myproject.myfolder.mymodule"
      ]
    }