question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Test Discovery fails with ModuleNotFoundError while command is working on console

See original GitHub issue

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.languageServer setting: Default
Output for Python in the Output panel (ViewOutput, 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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
multimericcommented, Aug 3, 2022

I’ve been hitting this error, which is super annoying. And all the mechanisms to configure this via the settings.json are mostly ignored by vscode. However one thing does work, which is the environment variables file.

Just by running echo 'PYTHONPATH=.' > .env at the root of my project, test discovery suddenly worked again.

The reason for this is that the .env file is automatically sourced by vscode when it does anything python related, and setting the PYTHONPATH like this ensures that your module is imported, since that’s kind of like the working directory for the interpreter.

0reactions
dovahgabcommented, Aug 1, 2022

hi, sorry for lagging, and thanks for your answer @karthiknadig not sure why it could use a different version, how can I confirm that? if you are referring to the ModuleNotFoundError and the python output I reported, the latter is what I can see from the tooltip on the python testing extension in vscode, where it fails discovering tests: image

I was looking for a solution on this for the same reason, debugging tests - at first I worked it out invoking pytest in a python script, such as

import pytest
retcode = pytest.main(["tests"])

and then debugging it. @g1augusto thanks! it works for me as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

pytest fails due to ModuleNotFoundError, but works when ...
I'm working with VS Code on Windows 10 within the built-in console. The command pytest does not work in neither scenario: neither in...
Read more >
Testing Python in Visual Studio Code
From the Command Palette, by running any of the following commands: Test: Run All Tests - Runs all tests that have been discovered....
Read more >
PyCharm - Run/Debug Configuration: Python Unit Test
In this text field, specify the pattern that describes all the tests in the required location. This parameter corresponds to the -p, --pattern ......
Read more >
pytest: ModuleNotFoundError: No module named 'requests'
If your project is using a module that is only installed in your virtual environment, and you're using a system-wide pytest, it won't...
Read more >
Python import: Advanced Techniques and Tips
Note: You might get a ModuleNotFoundError or an ImportError when running the code yourself. This is because serializers isn't in your Python import...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found