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.
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
python - "pytest" is not accessed / Import "pytest" could not be resolved - Pylance - Stack Overflow
Installed packaged not being 'resolved' - Brownie and PyTest
Pytest Installed, but VS Code Won't Access Pytest
pytest cannot import module while python can - Stack Overflow
Videos
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"
I tried h. deville fletcher's answer and it didn't seem to work for me, but I could have messed up in there too. However, in the process, I remembered that I hadn't changed my interpreter. Once I changed the interpreter to my pipenv shell that fixed my problem.
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 ==============================
update PYTHONPATH till src folder
export PYTHONPATH=/tmp/pycharm_project_968/src
This is by far the weirdest error i've ever seen. I won't bother with code cause it's easier with a screenshot IMO
I've tried it with multiple iterations (my_weather vs weather or w_eather vs weather) and somehow after trying a gazilliion things (you can see in my console how many times i've tried adding the PYTHONPATH) the only things that matters is... an underscore?
Please tell me whats up so I can go to sleep tonight.