vscode-python: Test Discovery fails with ModuleNotFoundError while command is working on console
Issue Type: Bug
Behaviour
Expected vs. Actual
note: issue similar to #10005 Tests discovery should detect tests, while it does not.
Steps to reproduce:
Project setup, in a basic pipenv environment:
│ ├── __init__.py
│ ├── mymodule
├── tests
│ ├── __init__.py
│ ├── test_mymodule.py
Test discovery on the testing extension fails with
ModuleNotFoundError: No module named 'mymodule'
Manual run with
pipenv run python3 -m pytest tests/test_*.py
works well.
setting.json content:
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
no pytest config file or so.
Diagnostic data
- Python version (& distribution if applicable, e.g. Anaconda): 3.8.10
- Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Global
- Value of the
python.languageServersetting: Default
Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)
==================================== ERRORS ====================================
________________ ERROR collecting tests/test_inspection_unit.py ________________
ImportError while importing test module 'path/to/file/test_1.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/test_1.py:2: in <module>
from mymodule import class1
E ModuleNotFoundError: No module named 'mymodule'
User Settings
languageServer: "Pylance"
testing
• pytestArgs: "<placeholder>"
• pytestEnabled: true
Extension version: 2022.10.1 VS Code version: Code 1.69.1 (b06ae3b2d2dbfe28bca3134cc6be65935cdfea6a, 2022-07-12T08:28:29.328Z) OS version: Linux x64 5.15.0-41-generic Restricted Mode: No
System Info
| Item | Value |
|---|---|
| CPUs | AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx (8 x 1207) |
| GPU Status | 2d_canvas: enabled canvas_oop_rasterization: disabled_off direct_rendering_display_compositor: disabled_off_ok gpu_compositing: enabled multiple_raster_threads: enabled_on opengl: enabled_on rasterization: enabled raw_draw: disabled_off_ok skia_renderer: enabled_on video_decode: disabled_software video_encode: disabled_software vulkan: disabled_off webgl: enabled webgl2: enabled |
| Load (avg) | 2, 2, 2 |
| Memory (System) | 6.72GB (0.57GB free) |
| Process Argv | –unity-launch --crash-reporter-id 70910446-bfe6-4cdf-a0cf-0efcd70e2fe8 |
| Screen Reader | no |
| VM | 0% |
| DESKTOP_SESSION | ubuntu |
| XDG_CURRENT_DESKTOP | Unity |
| XDG_SESSION_DESKTOP | ubuntu |
| XDG_SESSION_TYPE | x11 |
A/B Experiments
vsliv368:30146709
vsreu685:30147344
python383:30185418
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
pythonvspyl392:30443607
vserr242:30382549
pythontb:30283811
vsjup518:30340749
pythonvspyt551:30345470
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
pythondataviewer:30285071
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593cf:30376535
vsc1dst:30438360
pythonvs932:30410667
wslgetstarted:30449410
vscscmwlcmt:30465135
cppdebug:30492333
pylanb8912cf:30520717
vsclangdf:30486550
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (4 by maintainers)
I’ve been hitting this error, which is super annoying. And all the mechanisms to configure this via the
settings.jsonare mostly ignored by vscode. However one thing does work, which is the environment variables file.Just by running
echo 'PYTHONPATH=.' > .envat the root of my project, test discovery suddenly worked again.The reason for this is that the
.envfile is automatically sourced by vscode when it does anything python related, and setting thePYTHONPATHlike this ensures that your module is imported, since that’s kind of like the working directory for the interpreter.I have resolved this by adding a pyproject.toml in the Workspace root folder with the following data:
give it a try