You could select your python interpreter in VSCode by shortcuts "Ctrl+Shift+P" and type "Python: Select Interpreter" and choose the python interpreter which you installed the package in.

If it still not work, you could run command pip install pytest in the terminal to install the package in the current interpreter.

Answer from MingJie-MSFT on Stack Overflow
🌐
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

Discussions

python - "pytest" is not accessed / Import "pytest" could not be resolved - Pylance - Stack Overflow
While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers. Closed 3 years ago. ... I haven't touched python in a long time and I forgot how much of a pain importing can be. So I'm using pylance and pipenv as my shell. I have pytest installed ... More on stackoverflow.com
🌐 stackoverflow.com
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
Pytest Installed, but VS Code Won't Access Pytest
Hi, I’m starting to learn how to write tests for my code. I’ve installed pytest on my device using pip. But VS Code won’t access it. I assume it’s something to do with the path. I’ve tried using different IDEs, but I can’t find one that will let me use pytest without installing it. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
September 12, 2023
pytest cannot import module while python can - Stack Overflow
I am working on a package in Python. I use virtualenv. I set the path to the root of the module in a .pth path in my virtualenv, so that I can import modules of the package while developing the cod... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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 ...
Author   liam-ot
🌐
Bobby Hadz
bobbyhadz.com › blog › python-no-module-named-pytest
ModuleNotFoundError: No module named 'pytest' in Python | bobbyhadz
April 8, 2024 - To solve the error, install the module by running the pip install pytest command. Open your terminal in your project's root directory and install the pytest package. ... Copied!# 👇️ In a virtual environment or using Python 2 pip install ...
🌐
freeCodeCamp
forum.freecodecamp.org › python
Pytest Installed, but VS Code Won't Access Pytest - Python - The freeCodeCamp Forum
September 12, 2023 - Hi, I’m starting to learn how to write tests for my code. I’ve installed pytest on my device using pip. But VS Code won’t access it. I assume it’s something to do with the path. I’ve tried using different IDEs, but I…
Find elsewhere
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
🌐
pytest
docs.pytest.org › en › stable › explanation › pythonpath.html
pytest import mechanisms and sys.path/PYTHONPATH - pytest documentation
Added in version 6.0. ... Initially ... for the foreseeable future. ... By default, pytest will not attempt to resolve namespace packages automatically, but that can be changed via the consider_namespace_packages configuration variable....
🌐
GitHub
github.com › microsoft › pyright › discussions › 8847
Warning on pytest import · microsoft/pyright · Discussion #8847
Please reload this page. ... If you set the "verboseOutput" config setting to true, you should see a bunch of additional logging output that may help you diagnose the import resolution issue.
Author   microsoft
🌐
Databricks
community.databricks.com › t5 › get-started-discussions › pytest-module-not-found-error › td-p › 113216
PYTEST: Module not found error - Databricks Community - 113216
November 12, 2025 - Python uses the current working directory (os.getcwd()) and the PYTHONPATH to resolve imports. When you run pytest inside a notebook, the current directory might not be set as expected, so relative imports or direct module names can fail.
🌐
Finxter
blog.finxter.com › home › learn python blog › [fixed] no module named ‘pytest’
[Fixed] No module named 'pytest' - Be on the Right Side of Change
August 24, 2023 - Quick Fix: Python raises the ImportError: No module named 'pytest' when it cannot find the library pytest. The most frequent source of this error is that you haven’t installed pytest explicitly with pip install pytest. Alternatively, you may have different Python versions on your computer, and pytest is not installed for the particular version you’re using.
🌐
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
🌐
Index.dev
index.dev › blog › resolve-pytest-cannot-find-resource-errors-src-directory
How to Resolve Pytest 'Cannot Find Resource' Errors in src Directory
September 23, 2024 - Change import module to from my_package import module. ... Run export PYTHONPATH=$PYTHONPATH:$(pwd)/src before running pytest. ... Ensure that src/my_package/__init__.py exists. ... Make sure your pytest.ini is properly configured.
🌐
Pytest with Eric
pytest-with-eric.com › introduction › pytest-command-not-found
7 Simple Ways To Fix The "Pytest Command Not Found" Error | Pytest with Eric
November 24, 2023 - Here are some ideas to debug and resolve this issue. First, ensure that pytest is installed in your Python environment. You can check this by running either of the below commands.
🌐
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...
🌐
pytest
docs.pytest.org › en › 7.1.x › explanation › pythonpath.html
pytest import mechanisms and sys.path/PYTHONPATH — pytest documentation
For this reason this doesn’t require test module names to be unique. One drawback however is that test modules are non-importable by each other. Also, utility modules in the tests directories are not automatically importable because the tests directory is no longer added to sys.path.
🌐
Kedro
linen-slack.kedro.org › t › 16106926 › hello-i-have-a-question-about-pytest-import-error-this-is-my
Hello I have a question about pytest import error This is my Kedro #questions
December 5, 2023 - DeprecationWarning: Command 'kedro test' is deprecated and will not be available from Kedro 0.19.0. Use the command 'pytest' instead. ... [SOLVED] pytest scans directories until it finds a directory where there is no init.py We need an init.py in ... /src/ (not sure why we had one there in the first place) This would then mirror also the structure in https://github.com/WaylonWalker/kedro-spaceflights/tree/feature/project-template/src Then it should work to just import the package_name under src since it automatically then takes the src folder as root.
🌐
DEV Community
dev.to › shehanat › solved-pytest-error-importerror-error-importing-plugin-no-module-named--5840
[Solved] Pytest Error: ImportError: Error importing plugin '': No module named ... - DEV Community
May 22, 2022 - Add the prefix python -m before the usual pytest command like so: python -m pytest · Well this is the end of the article, if you have any questions or concerns please feel free to post a comment in this article and I will get back to you if ...