question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pytest fails module not found, even tho tests are discovered & works in debug,

See original GitHub issue

Thank you for your help! I have search many issues, and tried many solutions, but I can’t figure it out. I understand this is OSS, and no one may have time or interest to help me. That’s okay. I am very grateful for vscode-python, and appreciate your time.

We have written the needed data into your cl

  • VS Code Version: 1.53.2
  • OS Version: Windows 10.0.17763
  • Python: 3.6.9 from python.org, running in venv
  • pytest 6.2.2

Version: 1.53.2 (user setup) Commit: 622cb03f7e070a9670c94bae1a45d78d7181fbd4 Date: 2021-02-11T11:48:04.245Z Electron: 11.2.1 Chrome: 87.0.4280.141 Node.js: 12.18.3 V8: 8.7.220.31-electron.0 OS: Windows_NT x64 10.0.17763

Steps to Reproduce:

  1. use python 3.6 to create a virtualenv: $py -3.6 -m venv venv
  2. install requirements for pvlib and install pvlib in editable mode: pip install --no-deps -e .
  3. open pvlib folder in vscode, it chooses venv automatically, yay!
  4. see test are all discovered
  5. try to run tests, select pytest as test runner, use default setup.cfg as only setting
  6. pick any test and run it, but it fails to import pvlib in conftest.py
ImportError while loading conftest 'c:\Users\mikm\Projects\pvlib-python\pvlib\tests\conftest.py'.
pvlib\tests\conftest.py:10: in <module>
    import pvlib
E   ModuleNotFoundError: No module named 'pvlib'
Error: TypeError: Cannot read property 'testsuites' of null
  1. run the same test again using debug, and it works!
  2. try it in the console or a windows CMD, and it works! (venv) $ pytest pvlib/tests/test_infinite_sheds.py::test_gcr_prime

Does this issue occur when all extensions are disabled?: Yes/No

If I disable extensions, then the Python extension isn’t loaded, and so no tests are discovered, and so I can’t run them.

I have the following extensions installed and enabled: Python, Remote-WSL, Jupyter, Git History, C#, and C/C++

If I disable everything but Python and Jupyter (I cannot disable only Jupyter), the tests are still discovered, but still do not run.

Related Issues

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
wholmgrencommented, Feb 23, 2021

@mikofski I see the same behavior on my mac in my conda environment and having installed pvlib using pip install -e .. It’s been awhile since I’ve used vscode in this way so I didn’t notice when it broke (for no particular reason I am in the habit of using vscode’s debug test or running from the command line). I’ll guess it worked about 3 months ago. Sorry I didn’t actually try this when you first reported the issue - I initially understood it to be limited to virtualenv.

1reaction
FrancescoValeriocommented, Apr 28, 2021

FYI: I’ve tried the latest insider’s build of vscode and the python extension and set "python.useIsolation": false in my .vscode/settings.json and the same error still occurs.

I believe you need to set "python.useIsolation": false in your User Settings not your Workspace settings in VS Code Insiders. #14570 (comment)

That worked for me.

I would like to add that this also solved my bug. This being that pytest or in other words testing does not respect PYTHONPATH when it is in the root directory.

The fix, that is, adding “python.useIsolation”: false` to Preferences: Open Settings (JSON) solved the following issue:

  1. create a parent directory example_parent
  2. create a directory named foo and at the same level create a directory called test
  3. within the foo directory create a main.py file containing the following code:
def return_4():
    return 4
  1. within the test directory create a file called test_file.py containing the following code:
from foo.main import return_4

def test_4():
    assert return_4() != 4
  1. add the parent directory that foo and test reside in to your PYTHONPATH (with Mac this is done through sudo nano /private/etc/zprofile and then add : export PYTHONPATH=~/folder/to/example_parent

  2. open vscode

  3. within vscode open example_parent

  4. press ctrl+shift+p and then type python all tests

  5. enable and configure pytest in the root directory

  6. run pytest within vscode through the python run all tests setting with vscode opened in root

  7. You will receive the following error

python /Users/francesco/.vscode/extensions/ms-python.python-2021.4.765268190/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /Users/francesco/Work/example_parent -s --cache-clear .
Test Discovery failed: 
Error: ============================= test session starts ==============================
platform darwin -- Python 3.8.2, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: /Users/francesco/Work/example_parent
plugins: xdist-2.2.1, forked-1.3.0
collected 0 items / 1 error

==================================== ERRORS ====================================
_____________________ ERROR collecting tests/test_file.py ______________________
ImportError while importing test module '/Users/francesco/Work/example_parent/tests/test_file.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_file.py:2: in <module>
    from foo.main import return_4
E   ModuleNotFoundError: No module named 'foo'
=========================== short test summary info ============================
ERROR tests/test_file.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
===================== no tests collected, 1 error in 0.08s =====================

Traceback (most recent call last):
  File "/Users/francesco/.vscode/extensions/ms-python.python-2021.4.765268190/pythonFiles/testing_tools/run_adapter.py", line 22, in <module>
    main(tool, cmd, subargs, toolargs)
  File "/Users/francesco/.vscode/extensions/ms-python.python-2021.4.765268190/pythonFiles/testing_tools/adapter/__main__.py", line 100, in main
    parents, result = run(toolargs, **subargs)
  File "/Users/francesco/.vscode/extensions/ms-python.python-2021.4.765268190/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 44, in discover
    raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 2)
Read more comments on GitHub >

github_iconTop Results From Across the Web

VSCode pytest test discovery fails - python - Stack Overflow
As a noob, I was putting my unit tests inside the module files, since I was following the pattern of unittest , and...
Read more >
How to handle test failures — pytest documentation
This will invoke the Python debugger on every failure (or KeyboardInterrupt). Often you might only want to do this for the first failing...
Read more >
Debugger doesn't work when called on Pytest test ... - YouTrack
It seems that in debug mode it cannot find a package that it can find when I just run the test. Could it...
Read more >
Settings Reference for Python - Visual Studio Code
This page describes the key settings you can work with. For general information about working with settings in VS Code, refer to User...
Read more >
Effective Python Testing With Pytest
In this tutorial, you'll learn how to take your testing to the next level with pytest. You'll cover intermediate and advanced pytest ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found