vscode import error for python module
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.
- Press
Ctrl+Shift+P
- Type:
Configure Language Specific Setting
- Then select
Python
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)
- If yes, then delete or comment this line, save the file and reload VScode.
- 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?
Chandan Nayak
I have no special talents. I am only passionately curious. -Albert Einstein
Updated on February 05, 2021Comments
-
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 about 4 yearsOut of interest, why the downvote? I tried to answer correctly and point out things that could cause the problem?
-
karu over 3 yearsI also needed to restart VS Code afterwards.
-
James Huang over 3 yearsWhere can I find launch.json?
-
Yuvraj Singh Jadon about 3 years@JamesHuang press ctrl + shift + p and type launch.json and then hit enter.
-
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 about 3 yearslaunch.json is no longer found in latest vscode.
-
Antonio Sesto almost 3 yearswhere is launch.json located?
-
Juan almost 3 yearsThat did it for me too, thanks. It started failing out of the blue after the weekend. I guess a VS update
-
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 almost 3 yearsIf your hurt is like mine - What is
python.jedi
doing? See this -
Chris Zuidema over 2 yearsFor me this was the correct answer.
-
jbchurchill about 2 yearsWould it be safe to put this directly in settings.json?