3

I am running test discovery in vscode with pytest in the output shows me this command:

python /home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /home/dave/PythonProjects/pytest_test -s --cache-clear .

Executing this command in the vscode shell leads to an import error.

dave@dave-desktop  ~/PythonProjects/pytest_test   master  python /home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /home/dave/PythonProjects/pytest_test -s --cache-clear .
Traceback (most recent call last):
  File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/run_adapter.py", line 17, in <module>
    from testing_tools.adapter.__main__ import parse_args, main
  File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/adapter/__main__.py", line 9, in <module>
    from . import pytest, report
  File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/adapter/pytest/__init__.py", line 7, in <module>
    from ._discovery import discover
  File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 8, in <module>
    import pytest
ImportError: No module named pytest

Why does this show an error? Is this command executed from another directory. This is very frustrating, because i have no chance of analysing the error this way.

David
  • 2,926
  • 1
  • 27
  • 61

2 Answers2

1

You are probably actually invoking different Python interpreters/environments in each methods. VSCode docs say:

By default, the Python extension looks for and uses the first Python interpreter it finds in the system path.

But there are several ways of overriding this, manually selecting the interpreter is the most obvious.

To debug, I suggest you temporarily put the following lines at the very top of run_adapter.py (might wanna back it up first, as it is part of the VS-code Python extension):

import sys
print(f'interpreter: {sys.executable}')

This will print the path of your interpreter just before the import fails.

You might also want to take a look at this question.

bavaza
  • 10,319
  • 10
  • 64
  • 103
1

I couldn't see if in the comments above you answered whether the virtual environment was activated or not. It could happen that VSCode is executing the command from a place where the python executable and the module pytest both exist.

One option would be to install pytest globally and see if that helps (you can uninstall it later to clean your system, if needed).

Otherwise, be sure to create a virtual environment, activate it and install pytest on it before running your command.

python3 -m venv /path/to/new/virtual/environment (e.g. ./venv)
./venv/bin/activate
pip3 install pytest

Also, if you have Python 2 and 3 in the same environment, that could cause conflicts. Can you check whether the correct version is used by VSCode (3 from your example, I guess)? You can do Ctrl + Shift + p and select "Python: Select Intepreter" to select the correct version.

Uyric
  • 646
  • 7
  • 17