VSCode does not run pytest properly from virtual environment

Viewed 3346

I just discovered that vscode is able to run pytest directly out of the editor. That's pretty awesome - if it would work.

Currently I'm working on a project within a virtual environment, which is properly configured in vscode (and activated). pytest is installed in the virtual environment and activated as well, the tests I wrote are discovered as expected. But whenever I run a test, it says, that it does not find the module I'm currently working on and a ModuleNotFoundError is raised.

I have installed this module with pip install -e .. Running the same tests on a normal terminal console outside vscode works like expected.

It does not make a difference if pytest is enabled for the workspache only and/or for the user.

Sample code:

def test_something():
   from mymodule.config import Config
   ...

Output:

E       ModuleNotFoundError: No module named 'mymodule'

Pip sample output:

mymodule               0.1.0     /path/to/mymodule

Stange is also, that vscode runs the correct pytest script (it is not installed globally, only in this environment) with all installed plugins (i.e. pytest-datafiles).

Any ideas why?

Regards, Thomas

1 Answers

I played around a little bit with path settings. It finally started to work by using the full path to pytest as workspace setting.

"python.testing.pytestPath": "/home/thomas/.virtualenvs/edis/bin/pytest"

Don't ask me why VSCode is not working properly even though pytest is in the path of the virtual environment.

Regards, Thomas

Related