probably Pylance is having problems with the path for site-packages folder for your virtual environment and returns Import "pytest" could not be resolved, you need to set python path in this way:

"python.pythonPath": "/my/project/path/.venv/bin/python"
Answer from user11717481 on Stack Overflow
Discussions

Installed packaged not being 'resolved' - Brownie and PyTest
import brownie from brownie import accounts, Farm import pytest pylance doesn't recognize these libraries though the warning is ignored and compiles correctly as these are all up to date on my ... More on github.com
🌐 github.com
22
March 7, 2021
Import "[module]" could not be resolvedPylance (reportMissingImports)
I am learning a Python book, so I created folder for each chapter to storage code. Working directory is as follows: book └─chapter1 β”œβ”€a.py ─b.py b.py import a When I "open by code" in "book" folde... More on github.com
🌐 github.com
45
August 13, 2020
Pylance reports import Missing in inexistent file when git enabled
Type: Bug Create a workspace with git initialized Add some imports for local packages Git commit Extension version: 2024.5.1 VS Code version: Code 1.89.1 (dc96b837cf6bb4af9cd736aa3af08cf8279f7685, ... More on github.com
🌐 github.com
21
May 18, 2024
Cannot get pytest to work with VSCode
I was able to get test runs working in VS Code by adding _init_.py files (there should be double underscores on either side of the init) in the source root and in the tests directories. In the tests\init.py I included import sys sys.path.insert(0, r'C:\path_to_source_code') Idea came from stackoverflow Note: Trying hard to keep those dunders in the init.py file name but this editor won't allow it. More on reddit.com
🌐 r/vscode
8
7
November 25, 2019
🌐
GitHub
github.com β€Ί microsoft β€Ί pylance-release β€Ί issues β€Ί 2470
pytest missing from list of imports - reportMissingImports Β· Issue #2470 Β· microsoft/pylance-release
March 12, 2022 - You must be signed in to change notification settings Β· Fork 1k Β· Star 2k Β· New issueCopy link Β· New issueCopy link Β· Closed Β· Closed Β· pytest missing from list of imports - reportMissingImports#2470 Β· Copy link Β· Labels Β· waiting for user responseRequires more information from ...
Author Β  JohnSprunger
🌐
GitHub
github.com β€Ί microsoft β€Ί pylance-release β€Ί issues β€Ί 1020
Installed packaged not being 'resolved' - Brownie and PyTest Β· Issue #1020 Β· microsoft/pylance-release
March 7, 2021 - import brownie from brownie import accounts, Farm import pytest pylance doesn't recognize these libraries though the warning is ignored and compiles correctly as these are all up to date on my machine, i understand i can change the confi...
Author Β  liam-ot
🌐
Bobby Hadz
bobbyhadz.com β€Ί blog β€Ί python-no-module-named-pytest
ModuleNotFoundError: No module named 'pytest' in Python | bobbyhadz
April 8, 2024 - The error "Import "pytest" could not be resolved from source Pylance" occurs when the pytest module is not installed or you have selected the incorrect Python interpreter in your IDE (e.g.
🌐
GitHub
github.com β€Ί microsoft β€Ί pylance-release β€Ί issues β€Ί 5895
Pylance reports import Missing in inexistent file when git enabled Β· Issue #5895 Β· microsoft/pylance-release
May 18, 2024 - Type: Bug Create a workspace with git initialized Add some imports for local packages Git commit Extension version: 2024.5.1 VS Code version: Code 1.89.1 (dc96b837cf6bb4af9cd736aa3af08cf8279f7685, ...
Published Β  May 18, 2024
Author Β  Saltsmart
🌐
Reddit
reddit.com β€Ί r/vscode β€Ί cannot get pytest to work with vscode
r/vscode on Reddit: Cannot get pytest to work with VSCode
November 25, 2019 -

I am trying to get pytest to work. I am using VSCode, Anaconda and the Python extension on Windows 10 (all latest versions) with:

  • pytest 5.2.4.

  • Python 3.7

I am probably using the second most simplest use case. I have all my test files in a subdirectory called tests in the root directory of my application source code.

I can get test discovery and execution to work when I am at a command prompt with the activated anaconda environment and type: python -m . Pytest discovers all the tests and runs them with no problems whatsoever.

If I click on the Test discovery in VSCode, pytest finds all the test files but there are all kinds of ModuleNotFoundError errors. Example error output:

_____________________ ERROR collecting tests/test_acsr.py _____________________
ImportError while importing test module 'C:\path_to_source_code\tests\test_acsr.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests\test_acsr.py:6: in <module>
    from acsr import is_pure_ground_run
E   ModuleNotFoundError: No module named 'acsr'

The acsr.py file is in the root of my application (parent directory of tests) and contains the functions I wish to test.

I checked the VS Code, and pytest documentation to see if I was missing some configuration options but didn't find anything. It appears that under VS Code, pytest can't figure out the sys.path or something. I feel like I am going around in circles trying to get this working as none of the answers on stackoverflow or anywhere else seem to fit this simple use case.

If anyone can supply me with a fix or has a link to a comprehensive tutorial that helps with pytest setup in VS Code I would be greatly appreciated.

PS I had it working in VS Code months ago, and I don't remember having to do any special configuration beyond what was outlined in the docs. It just stopped working one day, perhaps after an update, not sure. When it was working I thought it was pretty awesome.

Find elsewhere
🌐
GitHub
gist.github.com β€Ί krisbolton β€Ί 20159d66f1d919c9c2380c96b6ac3915
VSCode import could not be resolved by Pylance (reportMissingImports) Β· GitHub
The squiggly lines and the error should have gone. Pylance now knows where to find the module you were attempting to import.
Top answer
1 of 15
2

Kept everything same and just added a blank test file at the root folder .. Solved it

Here are the findings, this problem really bugged me for a while. My folder structure was

mathapp/
    - server.py  
    - configuration.py 
    - __init__.py 
    - static/ 
       - home.html  
tests/            
    - functional 
       - test_errors.py 
    - unit  
       - test_add.py

and pytest would complain with the ModuleNotFoundError and gives the hint:

make sure your test modules/packages have valid Python names.

I introduced a mock test file at the same level as mathsapp and tests directory. The file contained nothing. Now pytest does not complain.

Result without the file

$ pytest
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\mak2006\workspace\0github\python-rest-app-cont
collected 1 item / 1 error

=================================== ERRORS ====================================
_______________ ERROR collecting tests/functional/test_func.py ________________
ImportError while importing test module 'C:\mainak\workspace\0github\python-rest-app-cont\tests\functional\test_func.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests\functional\test_func.py:4: in <module>
    from mathapp.service import sum
E   ModuleNotFoundError: No module named 'mathapp'
=========================== short test summary info ===========================
ERROR tests/functional/test_func.py
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
============================== 1 error in 0.24s ===============================

Results with the file

$ pytest
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\mak2006\workspace\0github\python-rest-app-cont
collected 2 items

tests\functional\test_func.py .                                          [ 50%]
tests\unit\test_unit.py .                                                [100%]

============================== 2 passed in 0.11s ==============================
2 of 15
2

update PYTHONPATH till src folder

export PYTHONPATH=/tmp/pycharm_project_968/src
🌐
Python Forum
python-forum.io β€Ί thread-40713.html
Pytest Installed, but VS Code Won’t Access Pytest
September 12, 2023 - Hi, Sorry, I understand questions about installing packages and problems with path directories are common, but I've tried everything and really need help. I’m starting to learn how to write tests for my code. I’ve installed pytest on my device...
🌐
Stack Exchange
devops.stackexchange.com β€Ί questions β€Ί 16370 β€Ί vscode-pytest-gives-me-an-error-when-importing
python - Vscode/pytest gives me an error when importing - DevOps Stack Exchange
August 2, 2022 - I asked this question on the regular stackoverflow but i think you guys will be much more competent about this topic. My guess is that is some sort of python setup issue. When I write an import line like this from mypackage import something and run it using vscode tools it gives my an error, when I run it using venv python it works. Same for pytest, I have to run it like this: python3 -m pytest tests for it to work.
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί vscode and pytest not recognizing imports
r/learnpython on Reddit: VSCode and pytest not recognizing imports
April 19, 2025 -

So I'm migrating my code to a new project format after learning about how they should be formatted for release. I'm using UV to create the .git-ignore and all the other goodies it does. The package is called cmo. I'm trying to run tests on some of the code and resolve imports.

So as an example: I have cmo/src/data/doctrine/air_operations_tempo. And I have a file cmo/src/helpers/values/get_item_from_menu with the function get_item_from_menu.

air_operations_tempo imports it but is getting an error that neither com/src/etc. nor src/helpers/etc. work as a valid import path.

Also, trying to import air_operations_tempo into cmo/tests/data/doctrine/test_air_operations_tempo doesn't work either with cmo/src/etc. nor src/data/etc.

I am at a loss it works on the old code but not anymore. Any help would be GREATLY appreciated. I am at wits end. It's probably something simple knowing my luck.

A picture of the file structure

🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί tagged β€Ί python-import
Newest 'python-import' Questions - Page 2 - Stack Overflow
I recently came to know that modules like os are imported way before a user python program starts running and therfore we cannot import a custom file named os even though it's not in the sys....
🌐
Sentry
sentry.io β€Ί sentry answers β€Ί vs code β€Ί fix pylance resolvemissingimports in vs code
Fix Pylance resolveMissingImports in VS Code | Sentry
July 15, 2024 - If you previously installed fastapi to the virtual environment, the resolveMissingImports error should now disappear. Otherwise, run pip install fastapi in your terminal before attempting to run your Python script.
🌐
GitHub
github.com β€Ί microsoft β€Ί pylance-release β€Ί issues β€Ί 4369
Import "X" could not be resolvedPylancereportMissingImports Β· Issue #4369 Β· microsoft/pylance-release
May 15, 2023 - Type: Bug After updating to the latest VS code and pylance, my import doesn't work for the local module under the same project folder. It used to work well, but now it cannot detect the module. Ext...
Published Β  May 15, 2023
Author Β  joshua40302-intel