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 Overflowprobably 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.
Installed packaged not being 'resolved' - Brownie and PyTest
Import "[module]" could not be resolvedPylance (reportMissingImports)
Pylance reports import Missing in inexistent file when git enabled
Cannot get pytest to work with VSCode
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.
I'm trying to use torch in a python script but even though it's pip installed, pylance doesn't recognize it
https://imgur.com/EM5fEIo
Any advice on how to resolve this? Thanks.
I'm currently learning python and trying to import a module into my actual file. My code runs as its supposed to, but I keep getting this error message / warning. The module and the actual file are both in the same directory, and my Editor is vscode. I really appreciate any help, thanks in advance.
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
You should create a pyrightconfig.json file or pyproject.toml file at the root of your project. For example, if it's a Django project, you should have one of those files where manage.py is placed. Then, set include parameter and add the subdirectories (or app folders in Django terms).
You can consult this sample config file. See this issue ticket.
For example, if this were my project structure:
βββ manage.py
βββ movie
β βββ admin.py
β βββ apps.py
β βββ __init__.py
β βββ models.py
β βββ tests.py
β βββ views.py
βββ moviereviews
β βββ asgi.py
β βββ __init__.py
β βββ settings.py
β βββ urls.py
β βββ wsgi.py
βββ pyproject.toml
my pyproject.toml would be:
[tool.pyright]
include = ["movie", "moviereviews"]
If you are working within a Python virtual environment, set venvPath and venv. Consult the documentation for an exhaustive list of options.
Ok, a relative import as illustrated here was able to solve this. So in my case I should have
# MyModule_test.py
import pytest
from .. import MyModule
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