Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest run by test explorer of vscode does not find modules installed by pip in dev mode

Running tests on my project code installed with pip in developer mode (pip install -e .) does work by executing pytest from the command line. However, running the same tests within the same Python virtualenv using the Test Explorer UI of Visual Studio Code does not work, Python raises ModuleNotFoundErrors.

I confirmed that my package is installed by pip list -v, which prints:

myproject            0.0.0      /path/to/myproject

Furthermore, I wrote failing test to get the Python path during test execution:

import sys


def test_some_test():
    print(sys.executable)
    print(sys.path)

    assert False

This confirms that the Python executable is the one from the virtualenv where my project code is installed. But the sys.path differs between the Test Explorer run and the command line run. The Test Explorer run does not include the /path/to/myproject in the sys.path, whereas the command line run does. Obviously, if the project's path is included in the sys.path, the package can be imported. Why is it missing from the Test Explorer UI run? How to make the Test Explorer use the packages installed in develop mode with pip?

I should add that the modules I try to import in my tests are in a package themselves, called mylib. So the actual import in my test is like

from mylib import function_under_test


def test_function_under_test():
    ...
like image 788
hintze Avatar asked Dec 28 '25 19:12

hintze


1 Answers

Update

This issue is fixed now. It just works.

Old Answer

Fiddling around with the issue more, I found that this is a bug in the Python extension for vscode, cf. issues 14579, and 14570 on github.com.

As a work-around one may either install the Little Fox Team Test Explorer UI extension, or an old version of the Microsoft Python extension (v2020.9.114305).

like image 84
hintze Avatar answered Dec 30 '25 18:12

hintze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!