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
Answer from EsmaeelE on Stack Overflow
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).

🌐
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.
🌐
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?

🌐
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...
🌐
Wise Owl
wiseowl.co.uk › python › blogs › python › vs-code › module-not-found-error
Python ModuleNotFoundError in VS Code using Code Runner
October 8, 2021 - If this worked, you've solved your problem! It did for me, although not before I'd tried too many other solutions involving .env files, .path files, launch.json settings, the PythonPath environment variable and other esoteric options.
🌐
Medium
medium.com › @mrayandutta › modulenotfounderror-and-python-custom-module-import-issues-in-vs-code-a-100-reliable-solution-05f4045bfa77
ModuleNotFoundError and Python Custom Module Import Issues in VS Code: A 100% Reliable Solution | by Ayan Dutta | Medium
December 26, 2024 - In this guide, we’ll demystify the causes of ModuleNotFoundError and walk you through a foolproof method to banish it forever using the PYTHONPATH environment variable.
🌐
Reddit
reddit.com › r/vscode › vscode isn't recognizing python modules?
r/vscode on Reddit: VSCode isn't Recognizing Python Modules?
July 6, 2023 -

Noob here. Currently dealing with a very simple yet frustratingly difficult to resolve problem importing python modules.

When using pip install in the terminal I get a Requirement Already Satisfied response in the console, however in code when Import the module, I get the error code pasted at the bottom. I also get this issue if I create a Python file and try to import it in my main.py

I think the issue is the path they're getting installed in is not where VSCode is looking, but I've been unable to find a way to resolve it. Either that, or pip install is using a different instance of python that isn't what VSCode is using? Very annoying. If you have a solution, please let me know! Thanks!

[{
"resource": "/Documents/Coding/VSCode/Projects/Photoeditor/PhotoEditor.py",
"owner": "_generated_diagnostic_collection_name_#0",
"code": {
"value": "reportMissingModuleSource",
"target": {
"$mid": 1,
"external": "https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportMissingModuleSource",
"path": "/microsoft/pyright/blob/main/docs/configuration.md",
"scheme": "https",
"authority": "github.com",
"fragment": "reportMissingModuleSource"
}
},
"severity": 4,
"message": "Import \"requests\" could not be resolved from source",
"source": "Pylance",
"startLineNumber": 2,
"startColumn": 8,
"endLineNumber": 2,
"endColumn": 16
}]

🌐
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 - If so hold ‘Ctrl+Shift+P’ in Windows or ‘Command+Shift+P’ in Mac to open up the context menu in Visual Studio Code. There search for ‘Python: Select Interpreter’
Find elsewhere
🌐
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
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.

🌐
GitHub
github.com › microsoft › vscode › issues › 103030
VS Code import Python module “ModuleNotFoundError” · Issue #103030 · microsoft/vscode
July 21, 2020 - I read a few posts about a similar bug occurring in VScode about a year ago. Does anyone know the solution? from folder1.python1 import hello hello() # Simply should print "hello" ModuleNotFoundError: No module named 'folder1' The path folder1.python1 is automatically intellisensed by the way.
Author   ngong123
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1160881 › visual-studio-python-module-not-found
Visual Studio Python Module Not Found - Microsoft Q&A
January 14, 2023 - If you added different versions of Python environments, please select the correct one and try to use this command py -m pip install pandas and install pandas again.
🌐
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.
🌐
Reddit
reddit.com › r/vscode › modulenotfounderror in visual studio for python
r/vscode on Reddit: ModuleNotFoundError in Visual studio for python
September 15, 2021 -

I was trying to follow this video posted: https://www.youtube.com/watch?v=ySRQsrkrVCU

I setup a virtual environment following this video: https://www.youtube.com/watch?v=Wuuiga0wKdQ

made the requrement.txt file here

[![enter image description here][1]][1]

and installed it here

[![enter image description here][2]][2]

now when I try to run my code I get an error saying Modulenotfound no module named 'yfinance'

I am confused as to what the problem is for this, I also had a general question about virtual environment in VS code, every time I start a new project, will I have to go through the video and make new path or is there a terminal code I can run?

[![enter image description here][3]][3]

[![enter image description here][4]][4]

[1]: https://i.stack.imgur.com/LIhcU.png

[2]: https://i.stack.imgur.com/j6imJ.png

[3]: https://i.stack.imgur.com/k5iOc.png

[4]: https://i.stack.imgur.com/rDFx1.png

🌐
Stack Exchange
sqa.stackexchange.com › questions › 50294 › python-vs-code-modulenotfounderror-no-module-named-occur
automated testing - Python + VS Code: ModuleNotFoundError: No module named ' ' occur - Software Quality Assurance & Testing Stack Exchange
June 24, 2022 - Facing issue with error : ModuleNotFoundError: No module named 'selenium' Basically, Error is not specific to module 'selenium'. Facing this issue in VS Code even when, I have selenium installed ...
🌐
GitHub
github.com › microsoft › vscode-jupyter › issues › 12976
ModuleNotFoundError while trying to import project modules · Issue #12976 · microsoft/vscode-jupyter
March 1, 2023 - Currently, I'm using PyCharm on my computer and have no issues with that repository but I get the same error when I do the same thing on my local VS Code. Also, I had the same issue in python script instead of Jupyter notebook which was resolved by adding the following line to the settings.json file in my Windows machine:
Author   BabakAmini
🌐
GitHub
github.com › microsoft › vscode-python › issues › 2675
ModuleNotFoundError when trying to debug with module on PythonPath · Issue #2675 · microsoft/vscode-python
September 24, 2018 - Module is correctly on PYTHONPATH, but when trying to debug a "ModuleNotFoundError" is raised. The module works fine in other debuggers like PyCharm or IDLE. Load the module and be able to use it. ... Try to debug the script. Exception is raised. Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)
Author   ArDragt
🌐
Ithy
ithy.com › article › fix-module-not-found-error-vscode-wvelaie0
Ithy - Resolving ModuleNotFoundError in VSCode: Comprehensive Guide
January 19, 2025 - Ensure proper project structure ... ModuleNotFoundError in Visual Studio Code (VSCode) typically occurs when Python cannot locate the specified module in the current environment....