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…
🌐
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 › 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'
Author   microsoft
🌐
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 - python -c "import sys;print(sys.executable)" python -c "import sys;print(sys.executable)" python -c "import sys;print(sys.executable)" python -c "import sys;print(sys.executable)" python ./.vscode-server/extensions/ms-python.python-2022.10.1/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir .
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 - pyvsc-run-isolated.py removes pip "editable" installations from sys.path, breaks pytest #14570
Author   microsoft
🌐
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, ...
🌐
Stack Overflow
stackoverflow.com › questions › 75995253 › trying-to-import-pytest-in-vs-code-but-it-is-not-importing-and-error-is-displaye › 75995515
python 3.x - Trying to import pyTest in VS code but it is not importing and error is displayed - Stack Overflow
pyTest was installed under home-brew where as python script were going under local folder. I changed the interpreter to homebrew path within VS code and all worked fine post that. This question can be closed.
🌐
pythontutorials
pythontutorials.net › blog › imports-break-vscode-testing-with-pytest
How to Fix Imports Breaking VSCode pytest Test Discovery in Your Project — pythontutorials.net
Virtual Environment Mismatch: VSCode is using the wrong Python interpreter (e.g., the global Python instead of your project’s virtual environment). Outdated or Misconfigured pytest Settings: pytest isn’t configured to look in the right directories for tests or project code. Let’s resolve these issues with actionable steps.
🌐
GitHub
github.com › microsoft › pylance-release › issues › 1020
Installed packaged not being 'resolved' - Brownie and PyTest · Issue #1020 · microsoft/pylance-release
March 7, 2021 - 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 config to not show me the warning, that is not the point, just wanted to point out that pylance seems to be having some issues with the brownie and pytest packages
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!
🌐
Reddit
reddit.com › r/vscode › cannot get testing tab/pylance(?) to detect .so file contents in testing tab or with intellisense
r/vscode on Reddit: Cannot get testing tab/pylance(?) to detect .so file contents in testing tab or with intellisense
October 11, 2024 -

I've got a bunch of files in a directory that I'm pointing to with both the PYTHONPATH and LD_LIBRARY_PATH. I'm able to work with and import these libraries basically everywhere else but the testing tab: This works on the terminal with my virtualenv, also works with my current settings in my vscode terminal, I can even run a script in vscode using its own debugger without having to modify launch.json and it will detect the import.

But any unit tests I execute can't find the package, and vscode/pylance marks the import line in the file editor with "could not be resolved" error. I'm guessing these things aren't seeing the paths early enough for it to use them properly, but I've tried a few things and nothing seems to work. At the very least it doesn't seem to work when I set them via .env file in my workspace file.

Edit: Tried setting the values in my .bashrc to make it as global and early as possible and that doesn't work either. Its like its actively choosing to ignore it.

Edit2: I think maybe what I need to do is somehow explicitly set the variables for test discovery? I can see in the output "all environment variables set for pytest discovery" and it seems to only be drawing on a sepcific subset of env variables its pulling from the system and ignoring others. The pythonpath in particular is pointing exclusively to somewhere in the extensions own files.

SOLUTION: So apparently it absolutely refused to even look at my PYTHONPATH or LD_LIBRARY_PATH variables I needed unless I specifically put them in a .env file and nowhere else, and it had to be in the directory of one specific module of my repository instead of the root directory, god why does this shit have to be so painfully specific.