Search code examples
pythonvisual-studio-codepytestvscode-debugger

VS Code Pytest Discovery Error Due to ModuleNotFoundError


I am trying to setup my VS Code debugger for pytest and am getting the error discovering pytests tests from the testing tab. The test I'm running works perfectly when run from the terminal but the VS debugger is not able to run it. In the testing tab, it directs me to the output where it shows a ModuleNotFoundError for logdna in the common.py file. That file has some other seemingly syntax errors but I believe VSCode is just not interpreting them correctly because the modules are in the code base. On the left you can see I ran pip3 install logdna to ensure logdna is actually installed.

Note that I am running inside a Poetry environment and have a .env in the project root directory.

Note also that I have reviewed and tried all answers provided here and none of the provided answers helped in my case: VSCode pytest test discovery fails

enter image description here


Solution

  • You are using virtualenv inside terminal. Here is clearly seeing in logs that python is installed inside /Users/.../pypoetry/virtualenvs/... folder.
    But VSCode using default python (/usr/local/bin/python3) or at least non virtualenv version

    You have to choose same interpreter in VSCode as terminal's one

    What is virtualenv?