Search code examples
pythonvisual-studio-codedebuggingtestingpython-unittest

ModuleNotFoundError when trying to debug a test


I am trying to debug a failing test method in VS Code but when I use the Debug Test action from the Testing sidebar or the context menu for the test method, it fails with the following error (found in the Debug Console):

E
======================================================================
ERROR: test_clients (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_clients
Traceback (most recent call last):
  File "/usr/lib/python3.8/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/home/melebius/git/my-project/my_project/tests/test_clients.py", line 9, in <module>
    from my_project.clients.base import Data
ModuleNotFoundError: No module named 'my_project'


----------------------------------------------------------------------
Ran 1 test in 0.007s

FAILED (errors=1)

The my_project folder is evidently not available to the Python interpreter. Unfortunately, I cannot find any information on where to define the path for a Debug Test run.

The ways I tried:

  • I can Debug Test by appending the path in my test file but I find this just a workaround because such a code should not be committed to Git.
  • I can run “plain” debug (without involving unittest) using the PYTHONPATH configuration in my .code-workspace file.
    • This is the most common advice I have found (1, 2). I have multiple debug launch configurations (I don’t know which one is used by the Debug Test action) but have the PYTHONPATH in all of them.
  • I have defined PYTHONPATH in the .env file of my project.
  • Run Test works well for my test method.
  • I can run my tests in the command line (python -m unittest) from the project’s root directory. If I want to run them directly from the tests folder, I have to export PYTHONPATH=/home/melebius/git/my-project first.

Additional information:

  • I use Python and Pylance extensions by Microsoft to provide the testing environment in VS Code.
  • I use venv for my project which seems to be set up correctly. (I have resolved all ModuleNotFoundError’s for external libraries by running pip install in the venv.)
  • I have multiple projects (folders) in my workspace and the debug launch configurations are currently set on the workspace level. I tried to create a launch.json file on the project level and put the PYTHONPATH there, too, but nothing changed. Workspace structure (simplified, showing just directories and mentioned files):
.
 |-my-project
 | |-my_project
 | | |-tests
 | | |-clients
 | | |-tools
 | |-.git
 | |-.vscode
 | | |-launch.json
 |-my-other-project
 | |-.git
 | |-...
 |-my-yet-another-project
 | |-.git
 | |-...
 |-my-projects.code-workspace

Where can I define the path for the Debug Test action in VS Code?


Solution

  • I solved the problem by installing my-project in editable mode to my Python virtual environment.

    pip install -e ./my-project