Lepidopterist's comment proved to be the game changer of hours of struggling...

Solution

  1. Press Ctrl + Shift + P to open the Command Palette

  2. Go to Users.setting.json

  3. 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 add

    gave...

     beyond top level package error
    
  • Absolute imports also failed

    from convenience.utilities import add

    gave...

     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
🌐
Reddit
reddit.com › r/learnpython › visual studio code modulenotfounderror: no module named
r/learnpython on Reddit: Visual Studio Code ModuleNotFoundError: No Module Named
March 25, 2020 -

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?

Top answer
1 of 9
32

Lepidopterist's comment proved to be the game changer of hours of struggling...

Solution

  1. Press Ctrl + Shift + P to open the Command Palette

  2. Go to Users.setting.json

  3. 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 add

    gave...

     beyond top level package error
    
  • Absolute imports also failed

    from convenience.utilities import add

    gave...

     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

2 of 9
9

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.

Discussions

visual studio code - vscode import error for python module - Stack Overflow
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: More on stackoverflow.com
🌐 stackoverflow.com
[Bug] Exception has occurred: ModuleNotFoundError No module named 'Module'
Environment data VS Code version: 1.28.0 Extension version (available under the Extensions sidebar): 2018.8.0 OS and version: Windows 10 Professional 64bit 10.0.17134 Build 17134 Python version (& ... More on github.com
🌐 github.com
4
March 11, 2019
python - Why do I get a "ModuleNotFoundError" in VS Code despite the fact that I already installed the module? - Stack Overflow
I'm trying to debug some Python code using VS Code. I'm getting the following error about a module that I am sure is already installed. Exception has occurred: ModuleNotFoundError No module named ' More on stackoverflow.com
🌐 stackoverflow.com
VSCODE IS DRIVING ME INSANE : ModuleNotFoundError: No module named '_tkinter'
Python doesn't ship with Tk/Tkinter by default on Ubuntu, it seems. Have you installed the python3-tk package? More on reddit.com
🌐 r/learnprogramming
11
0
July 11, 2022
🌐
Medium
medium.com › nerd-for-tech › import-errors-in-python-no-module-named-module-name-for-vs-code-887d1f78cf02
Import Errors in Python: No Module Named “Module_Name” For VS Code | by Dilmi Kottachchi | Nerd For Tech | Medium
June 12, 2021 - Import Errors in Python: No Module Named “Module_Name” For VS Code You may have come across the dreaded import error in python like the one below: No module named ----- This is actually a simple …
🌐
Python Forum
python-forum.io › thread-33822.html
[FIXED] User-defined module: ModuleNotFoundError error in VSCode but not in PyCharm
Dear all, My apologies in advance for such a simple question, but I've spent hours browsing on the Net w/o obtaining a solution. I am newbie to Python and coding, and I am working on a project for my studies. I am using VSCode casue PyCharm is too h...
🌐
k0nze
k0nze.dev › posts › python-relative-imports-vscode
Python Relative Imports in VSCode (Fix ModuleNotFoundError and Auto-completion) | k0nze
January 30, 2024 - There is a dirty fix to remove the ModuleNotFoundError by extending the PYTHONPATH inside of main.py. PYTHONPATH is an environment variable that holds paths to additional directories in which the Python interpreter will look into to find packages and modules. PYTHONPATH can be manually extended within a Python file using sys: However, this solution not only looks terrible, but it also has a lousy code design.
Find elsewhere
Top answer
1 of 16
72

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
2 of 16
55

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).

🌐
Reddit
reddit.com › r/learnprogramming › vscode is driving me insane : modulenotfounderror: no module named '_tkinter'
r/learnprogramming on Reddit: VSCODE IS DRIVING ME INSANE : ModuleNotFoundError: No module named '_tkinter'
July 11, 2022 -

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

🌐
YouTube
youtube.com › the code city
How to Solve ModuleNotFound Error in Python in VSCode (2023) - YouTube
In this video, I'll show you how you can fix the module not found error in visual studio code. VSCode is a popular IDE for python porgramming. The "module no...
Published   November 20, 2023
Views   7K
🌐
GitHub
github.com › microsoft › vscode-python › issues › 11410
Exception has occurred: ModuleNotFoundError No module named 'cv2' · Issue #11410 · microsoft/vscode-python
April 25, 2020 - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "pythonPath": "${config:python.pythonPath}", "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal" } ] } ... i try Debug this code in vs code but say : Exception has occurred: ModuleNotFoundError No module named 'cv2'
Author   mohammadsaleh40
🌐
freeCodeCamp
forum.freecodecamp.org › python
Error: ModuleNotFound. I've tried everything - Python - The freeCodeCamp Forum
September 26, 2022 - I’m trying to get PySimpleGui up and running in VS Code. I installed it using Pip. If I list the installed pip modules with python -m pip list, PySimpleGui version 4.60.3 is right there in the list. I found the file location and added it to the extensions path in settings.
🌐
Stack Overflow
stackoverflow.com › questions › 70980385 › vscode-modulenotfounderror-no-module-named-in-terminal › 70980566
python - VSCode ModuleNotFoundError: No module named <...> in Terminal - Stack Overflow
When I try to import the MWE below from the Terminal in VSCode, I get ModuleNotFoundError: No module named 'tqdm' temp.py: import tqdm print('hello') Here is my Terminal session: (mainenv) Larrys...
🌐
GitHub
github.com › microsoft › vscode-jupyter › issues › 12976
ModuleNotFoundError while trying to import project modules · Issue #12976 · microsoft/vscode-jupyter
March 1, 2023 - Use 'hmac-sha256' instead of '"hmac-sha256"' – or use CUnicode. warn( c:\Users\Babak\AppData\Local\Programs\Python\Python310\lib\site-packages\traitlets\traitlets.py:2151: FutureWarning: Supporting extra quotes around Bytes is deprecated in traitlets 5.0. Use 'f6eb4cb1-4f88-43b7-a8f5-042386291fae' instead of 'b"f6eb4cb1-4f88-43b7-a8f5-042386291fae"'. warn( info 22:26:11.700: Started Kernel Python 3.10.10 (pid: 9344) info 22:26:11.816: Process Execution: > ~\AppData\Local\Programs\Python\Python310\python.exe ~\.vscode\extensions\ms-toolsai.jupyter-2023.1.2010391206\pythonFiles\printJupyterDat
Author   BabakAmini
🌐
Saturn Cloud
saturncloud.io › blog › how-to-fix-the-modulenotfounderror-no-module-named-pandas-error-in-vs-code
How to Fix the ModuleNotFoundError No module named pandas Error in VS Code | Saturn Cloud Blog
October 30, 2023 - You should see the name of the Python environment in a dropdown menu. Make sure that the environment you’re using has Pandas installed. If you’ve installed Pandas in a different environment, you can switch to that environment in VS Code by clicking on the Python version in the status bar and a list of environment will show up. Then, choose the environment with Pandas installed. Another possible cause of the error is that the path to the Pandas module is not configured correctly.