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

🌐
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 - At the bottom of the vscode window you should see the environment listed. If its the wrong one click it and find the right one. Also your launch.json and setup file seems wrong. The program should probably be script or python3 ${file} in launch.json.
🌐
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.

🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Pytest Installed, but VS Code Won't Access Pytest - Curriculum Help - 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…
🌐
GitHub
github.com › microsoft › vscode-python › issues › 9450
Cannot discover with pytest in 2020-01 · Issue #9450 · microsoft/vscode-python
January 8, 2020 - import pytest, report File "c:\Users\Federico\.vscode\extensions\ms-python.python-2020.1.57204\pythonFiles\testing_tools\adapter\pytest\__init__.py", line 7, in <module> from ._discovery import discover File "c:\Users\Federico\.vscode\extensions\ms-python.python-2020.1.57204\pythonFiles\testing_tools\adapter\pytest\_discovery.py", line 8, in <module> import pytest ModuleNotFoundError: No module named 'pytest' with pytest installed in the base environment (using the base envornment or any other with pytest) I get the following error ·
Author   microsoft
🌐
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 ...
🌐
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...
Find elsewhere
🌐
GitHub
github.com › microsoft › pylance-release › issues › 4873
Import Errors in Visual Studio Code with Python · Issue #4873 · microsoft/pylance-release
The issue persists even with a fresh installation of Python and VSC, and with all Python-related folders deleted. The code runs without any import errors in PyCharm. The import statement works to import a function or class, but it is labeled as a defect import, and pytest does not detect it unless it is testing right out of the file.
Author   microsoft
🌐
GitHub
github.com › microsoft › vscode-python › issues › 19556
VSCode cannot detect pytest unit tests, even pytest has been installed, error comes from inside .vscode-serve folder, behind company's proxy · Issue #19556 · microsoft/vscode-python
July 26, 2022 - microsoft / vscode-python Public forked from DonJayamanne/pythonVSCode ... VSCode cannot detect pytest unit tests, even pytest has been installed, error comes from inside .vscode-serve folder, behind company's proxy#19556
Author   microsoft
🌐
GitHub
github.com › microsoft › vscode-python › issues › 15398
pytest fails module not found, even tho tests are discovered & works in debug, · Issue #15398 · microsoft/vscode-python
February 13, 2021 - If I disable everything but Python and Jupyter (I cannot disable only Jupyter), the tests are still discovered, but still do not run. pyvsc-run-isolated.py removes pip "editable" installations from sys.path, breaks pytest #14570
Author   microsoft
🌐
GitHub
github.com › microsoft › vscode-python › issues › 14579
pytest broken (run fails on module discovery, debug works) · Issue #14579 · microsoft/vscode-python
October 30, 2020 - This happens only when pytests are run via VS Code. If run via cmd, this is is not the case. Works on previous version: v2020.9.114305. I discovered that new plugin has different sys.path list.
Author   microsoft
🌐
GitHub
github.com › microsoft › vscode-python › issues › 6347
Test Discovery Fails - Reports as PyTest not installed · Issue #6347 · microsoft/vscode-python
June 26, 2019 - Testing was working yesterday; I signed off around 4:45pm EST, got up this morning and encountered issues discovering and importing the packages from vscode. To me it appears as though the root of the workspace is not being added to the path. Example: executing python -m pytest ETC from c:/projects/customers/CUSTOMERX/PROJECTX/ adds that to the path, specifying ./tests/ searches ./tests for tests and I can import the high level package from there.
Author   microsoft
🌐
GitHub
github.com › microsoft › vscode-python › issues › 24414
ImportError: Error importing plugin "vscode_pytest": No module named 'vscode_pytest' · Issue #24414 · microsoft/vscode-python
November 11, 2024 - 2024-11-11 12:17:09.746 [error] Traceback (most recent call last): File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 864, in import_plugin __import__(importspec) ModuleNotFoundError: No module named 'vscode_pytest' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "runpy.py", line 196, in _run_module_as_main File "runpy.py", line 86, in _run_code File "C:\Python\lib\site-packages\pytest\__main__.py", line 7, in <module> raise SystemExit(pytest.console_main()) File "C:\Python\lib\site-packages\_pytest\config\__init__.p
Author   microsoft
🌐
Stack Overflow
stackoverflow.com › questions › 68163930 › python-importerror-and-valueerror-in-vscode-pytest
visual studio code - Python ImportError and ValueError in VSCode/pytest - Stack Overflow
I had to use absolute imports in all files: from folder.file import function. Read about absolute vs. relative imports here. If you open your code in root - VSCode will now find your imports! ... Finally, running pytest works!
🌐
pythontutorials
pythontutorials.net › blog › imports-break-vscode-testing-with-pytest
How to Fix Imports Breaking VSCode pytest Test Discovery in Your Project — pythontutorials.net
Why this works: Absolute imports use the package name (my_project) to locate modules. As long as my_project is in Python’s path (configured via PYTHONPATH or VSCode settings), this will resolve.
🌐
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, ...
🌐
GitHub
github.com › Microsoft › vscode-python › issues › 4566
Cannot import "tests" module from my own workspace · Issue #4566 · microsoft/vscode-python
February 27, 2019 - With the latest version of the extension (2019.2.5416), there appears to be a set of "data science" tests under pythonFiles\tests that are causing a path conflict when we try to run our own integration "tests" using PyTest. Running the f...
Author   microsoft