vscode-python: VS Code python unittest fails to import modules outside tests folder during debug

Type: Bug

Since earlier this month, attempting to debug Python unittest tests in existing and new projects fails with an error similar to

ModuleNotFoundError: No module named '<file or folder name>'

for any test file trying to import something outside of the tests folder.

A basic project based on guidance in Testing Python in Visual Studio Code can be cloned from https://github.com/cfframe/PythonBasicTest.

Tests will run using the gui, but debugging a test generates this error in the DEBUG CONSOLE:

E
======================================================================
ERROR: test_unittest (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_unittest
Traceback (most recent call last):
  File "C:\Python\3.10.11\lib\unittest\loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "c:\GitLab\cfframe\BasicTest3\PythonBasicTest\tests\test_unittest.py", line 1, in <module>
    import inc_dec as inc_dec    # The code to test
ModuleNotFoundError: No module named 'inc_dec'


----------------------------------------------------------------------
Ran 1 test in 0.009s

FAILED (errors=1)

Versions:

  • VSCode versions 1.80.1, 1.80.0, 1.79.2, 1.79.1
  • Python 3.10.11, 3.10.6, 3.9.13
  • ms-python.python 2023.12.0
  • ms-python.vscode-pylance 2023.7.20

I have disabled all other extensions to try troubleshooting this.

I have no similar problems with PyCharm on the same projects.

Extension version: 2023.12.0 VS Code version: Code 1.80.1 (74f6148eb9ea00507ec113ec51c489d6ffb4b771, 2023-07-12T17:22:07.651Z) OS version: Windows_NT x64 10.0.19045 Modes:

System Info
Item Value
CPUs 11th Gen Intel® Core™ i5-1145G7 @ 2.60GHz (8 x 2611)
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
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) undefined
Memory (System) 15.69GB (6.25GB free)
Process Argv C:\GitLab\cfframe\BasicTest3\PythonBasicTest --crash-reporter-id 679c4d58-dda0-4be6-a133-afbb5e749e9c
Screen Reader no
VM 0%
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
vserr242cf:30382550
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
vscoreces:30445986
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593cf:30376535
pythonvs932:30410667
py29gd2263:30789496
vsclangdf:30486550
c4g48928:30535728
dsvsc012cf:30540253
pynewext54:30695312
azure-dev_surveyone:30548225
3biah626:30602489
pyind779:30671433
f6dab269:30613381
vscrp:30673768
pythonsymbol12:30671437
showlangstatbar:30737416
vsctsb:30748421
pythonms35:30701012
a2ce3375:30757347
pythonfmttext:30731395
pythoncmvfstrcf:30756944
fixshowwlkth:30771522
hideindicator:30785051
pythongtdpath:30769146
bgfeh915:30780428
pythonnosm12tcf:30779713
pythonidxpt:30784022
pythonnoceb:30776495
h7j2d465:30786200
dsvsc013cf:30789518

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 25 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Thank you for your issue report. We are looking into this now! In the meantime, you are likely on the new testing rewrite and this is why you saw a change in behavior since we just moved to this experiment to a larger group of people receiving it. You can opt out of the rewrite as I get this fix in by setting this in your user settings: "python.experiments.optOutFrom": ["pythonTestAdapter"],. If this doesn’t work let me know as this will mean it is a different issue. Thank you and I will send updates in this thread as I get a fix in.

Thank you for your issue report. We are looking into this now! In the meantime, you are likely on the new testing rewrite and this is why you saw a change in behavior since we just moved to this experiment to a larger group of people receiving it. You can opt out of the rewrite as I get this fix in by setting this in your user settings: "python.experiments.optOutFrom": ["pythonTestAdapter"],. If this doesn’t work let me know as this will mean it is a different issue. Thank you and I will send updates in this thread as I get a fix in.

For those of you coming across this error while in a docker container, You have to set this in your docker container’s settings: Settings -> Remote[...Container...] -> add the setting

This happened out of the blue for me. And I was pulling my hair out for few days. Thank for your help everyone.