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:
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.unittest
) using the PYTHONPATH
configuration in my .code-workspace
file.
PYTHONPATH
in the .env
file of my project.Run Test
works well for my test method.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:
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
.)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?
I solved the problem by installing my-project
in editable mode to my Python virtual environment.
pip install -e ./my-project