Lepidopterist's comment proved to be the game changer of hours of struggling...
Solution
Press Ctrl + Shift + P to open the Command Palette
Go to Users.setting.json
Add the following line:
"terminal.integrated.env.windows": { "PYTHONPATH": "${workspaceFolder}" }
For reference: Settings.json
Explanation
This was my project structure...
Project
I had three modules/packages inside my project!
- module_a + module_b: performing standalone tasks
- convenience: provides utility functions to all other modules
What I wanted to do!
- keep all utility functions inside the convenience module and import them to whichever module that requires them
What did not work!
Relative imports failed
from ..convenience.utilities import addgave...
beyond top level package errorAbsolute imports also failed
from convenience.utilities import addgave...
ModuleNotFoundError: No module named 'convenience'Adding Workspace directory to PYTHONPATH also failed
"env": {"PYTHONPATH": "${workspaceFolder}"}gave...
ModuleNotFoundError: No module named 'convenience'
Why did the solution (I mentioned at the start) work?
- Because I was running the module_a/task.py, using the integrated terminal. It makes sense that, Pythonpath needs to be appended for the terminal where we add the path of our workspace directory.
(One thing to note): My operating system was Windows, so it was...
"terminal.integrated.env.windows"
For Linux, it would be...
"terminal.integrated.env.linux"
Result after modifying Users.Setting.json:
Output
Answer from haider abbasi on Stack Overflow
I'm installing Python and VSCode on a new computer, and I remember the process being a little complicated last time. More than I remember, I guess, because I'm getting ModuleNotFoundError: No Module Named when importing any modules in VS Code.
I'm pretty sure these modules are installed correctly using pip in python, because when I run "import win32com" or "import pyinstaller" running python from the command prompt, I don't get an error.
I've selected a Python Interpreter (C:\Users\name\AppData\Local\Programs\Python\Python38-32\python.exe). Is there anything else I'm supposed to do to get this to work?
Lepidopterist's comment proved to be the game changer of hours of struggling...
Solution
Press Ctrl + Shift + P to open the Command Palette
Go to Users.setting.json
Add the following line:
"terminal.integrated.env.windows": { "PYTHONPATH": "${workspaceFolder}" }
For reference: Settings.json
Explanation
This was my project structure...
Project
I had three modules/packages inside my project!
- module_a + module_b: performing standalone tasks
- convenience: provides utility functions to all other modules
What I wanted to do!
- keep all utility functions inside the convenience module and import them to whichever module that requires them
What did not work!
Relative imports failed
from ..convenience.utilities import addgave...
beyond top level package errorAbsolute imports also failed
from convenience.utilities import addgave...
ModuleNotFoundError: No module named 'convenience'Adding Workspace directory to PYTHONPATH also failed
"env": {"PYTHONPATH": "${workspaceFolder}"}gave...
ModuleNotFoundError: No module named 'convenience'
Why did the solution (I mentioned at the start) work?
- Because I was running the module_a/task.py, using the integrated terminal. It makes sense that, Pythonpath needs to be appended for the terminal where we add the path of our workspace directory.
(One thing to note): My operating system was Windows, so it was...
"terminal.integrated.env.windows"
For Linux, it would be...
"terminal.integrated.env.linux"
Result after modifying Users.Setting.json:
Output
Make sure you are running from the package folder (not from package/module) if you want import module.calculations to work. You can also set the PYTHONPATH environment variable to the path to the package folder.
visual studio code - vscode import error for python module - Stack Overflow
[Bug] Exception has occurred: ModuleNotFoundError No module named 'Module'
python - Why do I get a "ModuleNotFoundError" in VS Code despite the fact that I already installed the module? - Stack Overflow
VSCODE IS DRIVING ME INSANE : ModuleNotFoundError: No module named '_tkinter'
Videos
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! :)
In your launch.json file, change env:{} to:
"env": {"PYTHONPATH": "${workspaceRoot}"}
After installing a new module via pip, if VS Code doesn't recognize it, then reloading VS Code may work.
Make sure the module is installed inside the virtual environment by creating and activating a virtualenv:
python3 -m venv env
source env/bin/activate
Use the correct way of installing a module with pip (Brett Cannon's article):
python3 -m pip install module_name
Replace the string "module_name" with your desired module name.
Reload VS Code by pressing Ctrl+Shift+P, and selecting Reload window.
Now it will know the new module and auto completion works.
Consider especially in new distribution like Debian 12 (Bookworm) with newer Python 3, always use a virtual environment to manage Python packages.
This is based on PEP 668, Marking Python base environments as “externally managed”.
For any new project, always create a new environment with:
python3 -m venv env
And activate it by running source env/bin/activate, and then install packages on it by pip.
Check if module_name is installed in current active virtualenv:
pip freeze
To deactivate the current active virtual environment, i.e., env run,
deactivate
Also it is worth mentioning to use the Python venv module, we must install it before.
For a Debian-based machine, here is the install command:
sudo apt install python3-venv
And same things for pip module for install a new python package.
For a Debian-based machine, here is the install command:
sudo apt install python3-pip
sudo pip install is most likely installing globally into a Python interpreter that is different than the one that you have selected in VS Code. Please select the Python interpreter you want to use and then install explicitly using that interpreter (if you're not using a virtual environment then use something like /path/to/python -m pip install SimpleITK, although I strongly recommend using a virtual environment and to not install packages globally).
This is on Ubuntu i'm learning linux so i installed vscode here because i like it, it's easy and all that, but i can't work with tkinter even after downloading tkinter and still nothing. Help please!
This is related to python to don't remove my post
EDIT : FOUND THE SOLUTION THANK YOU FOR HELPING
https://www.youtube.com/watch?v=GqTsFOtZiQI&ab_channel=GalvanizeDataScience
THIS IS THE SOLUTION
You can try the following:
{
"name": "Python: A name example",
"type": "python",
"request": "launch",
"program": "practice1/my_module.py",
"env": {
"PYTHONPATH": "${workspaceFolder}"
},
}
- Using
programinstead ofmoduleis almost the same. - You need to solve the
importsas if you had the package installed. Therfore you need to add aPYTHONPATH.
I have ran into the same issue. You may have two Python interpreters installed and only one has the modules you need. Try selecting another Python version by running from the command pallet (Ctl+Shift+p) then python: Select Interpreter and choose where your python lives. I had:
/usr/bin/python3
/usr/local/bin/python
It's easier than we imagine:

This image explains how to solve this problem.
The problem (at least in my case) was that I have installed a package under the default Python version but I have set the interpreter for the different Python version in Visual Studio Code (VS Code). There are 2 options to resolve this.
- Change the VS Code Interpreter: VS Code -> View -> Command Palette... (Ctrl+Shift+P) -> Python: Select Interpreter -> select "Python: Select Interpreter" (or Enter) -> select an interpreter based on our chosen Python version under which you have installed the package.
- Install package under the correct Python version which means to change your default Python version and repeat the process of installation again. To change your default Python version (for Windows 10): Right click on This PC -> Properties -> Advanced System Settings (in the right panel) -> Environment Variables -> System variables (the bottom part of the window) -> double-click on "Path" -> Select the 1st row for the wanted Python version and move it up and then do the same with the 2nd row. I recommend to restart (close and open again) your Command Prompt session if you want to see/work with the new default Python version.
Note on installation: Following command (in Command Prompt) worked for me: pip3 install pandas --user