vscode import error for python module

116,896

Solution 1

I tried to add this in my launch.json, then it works!

"env": {"PYTHONPATH": "${workspaceRoot}"}

below is my launch.json

        "name": "Python: Current File (Integrated Terminal)",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "cwd": "${workspaceRoot}",
        "env": {"PYTHONPATH": "${workspaceRoot}"},
        "console": "integratedTerminal"

wish it can help u! :)

Solution 2

The solution is given below just worked for me.

  1. Press Ctrl+Shift+P
  2. Type: Configure Language Specific Setting
  3. Then select Python
  4. settings.json will open. Check in this JSON file if there is a line like this:
{"python.jediEnabled": false}

(Press Ctrl+F and then paste the above line to find it quickly)

  1. If yes, then delete or comment this line, save the file and reload VScode.
  2. DONE!

Solution 3

In your launch.json file, change env:{} to:

"env": {"PYTHONPATH": "${workspaceRoot}"}

Solution 4

In my case, it's nothing to do with

"env": {"PYTHONPATH": "${workspaceRoot}"}

Here is my folder/module structure:

/Dev/csproj/deploy/test.py 
/Dev/csproj/util/utils.py

and in test.py, it imports the utils function

import sys
sys.path.append('../')
from util.utils import get_keyvault_secret

It has no issue if I run test.py in terminal folder /Dev/csproj/deploy/.
But if I want to debug test.py in VSCode (under workspaceRoot), I got the exception of "ModuleNotFoundError"
To fix it, I add this to my debug configuration launch.json

"cwd": "${workspaceRoot}\\Dev\\csproj\\deploy",

Solution 5

Thanks Honza Kalfus jankalfus

I have noticed that if I use File -> Close folder and then File -> Open Folder... and open the project folder again, the errors are gone. If I just restart VS Code instead, I keep getting the errors. I presume that some internal cache gets cleared?

Found here https://github.com/Microsoft/vscode/issues/10391

Share:
116,896
Chandan Nayak
Author by

Chandan Nayak

I have no special talents. I am only passionately curious. -Albert Einstein

Updated on February 05, 2021

Comments

  • Chandan Nayak
    Chandan Nayak about 3 years

    I am trying to do an import in python from one directory level up.

    import sys
    
    sys.path.append('..')
    from cn_modules import exception
    

    I get an Error from VSCode when I try to do Run Build Task as:

    ImportError: No module named cn_modules

    The same code works without any error from terminal (python).
    I face the problem when I try to run it from VSCode Run Build task.
    Any clue on what is wrong here?

    Have spent quiet some time but not able to resolve this, Any help is appreciated.


    NOTE: this works when i do debug using vscode too. Below are my config for launch.json and tasks.json

    launch.json

     {
            "version": "0.2.0",
            "configurations": [
                {
                    "name": "Python Console App",
                    "type": "python",
                    "request": "launch",
                    "stopOnEntry": true,
                    "program": "${file}",
                    "externalConsole": true,
                    "debugOptions": [
                        "WaitOnAbnormalExit",
                        "WaitOnNormalExit"
                    ],
                    "env": {},
                    "envFile": "${workspaceRoot}/.env",
                    "console":"integratedTerminal",
                    "pythonPath": "${config:python.pythonPath}"
                }
            ]
        }
    

    tasks.json

    {
            "version": "0.1.0",
            "command": "/usr/bin/python",
            "isShellCommand": true,
            "args": ["${file}"],
            "showOutput": "always",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "pythonPath": "${config:python.pythonPath}"
     }
    
  • Paulus
    Paulus about 4 years
    Out of interest, why the downvote? I tried to answer correctly and point out things that could cause the problem?
  • karu
    karu over 3 years
    I also needed to restart VS Code afterwards.
  • James Huang
    James Huang over 3 years
    Where can I find launch.json?
  • Yuvraj Singh Jadon
    Yuvraj Singh Jadon about 3 years
    @JamesHuang press ctrl + shift + p and type launch.json and then hit enter.
  • bwdm
    bwdm about 3 years
    @JamesHuang Configurations are defined in a launch.json file that's stored in a .vscode folder in your workspace. If it doesn't exist, it can be initialized via the debug menu from the sidebar.
  • John Jiang
    John Jiang about 3 years
    launch.json is no longer found in latest vscode.
  • Antonio Sesto
    Antonio Sesto almost 3 years
    where is launch.json located?
  • Juan
    Juan almost 3 years
    That did it for me too, thanks. It started failing out of the blue after the weekend. I guess a VS update
  • Raid
    Raid almost 3 years
    @JohnJiang If there's no .vscode directory you can just make one (in the project directory). Then you can create launch.json in there.
  • Timo
    Timo almost 3 years
    If your hurt is like mine - What is python.jedi doing? See this
  • Chris Zuidema
    Chris Zuidema over 2 years
    For me this was the correct answer.
  • jbchurchill
    jbchurchill about 2 years
    Would it be safe to put this directly in settings.json?