Skip to content

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

Closed

Description

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
Details

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
Details
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

Activity

  1. ghost added
    triage-neededNeeds assignment to the proper sub-team
    on Feb 13, 2021
  2. mikofski commented on Feb 15, 2021

    @mikofski
    Author

    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.

    However, if I re-install pvlib using python setup.py install now it works. So I cannot tell if this is a duplicate of #14570 or not. My guess is that it is a duplicate, but then I don't understand why using the insider's version breaks it.

    I'd also like to know how others are using pytest and virtualenvs in vscode?

  3. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on Feb 16, 2021
  4. ericsnowcurrently commented on Feb 17, 2021

    @ericsnowcurrently

    Mark Mikofski (@mikofski), thanks for letting us know about this. Please provide the content of the "Python" output panel and the "Python Test Log" output panel. Also, please provide the content of your settings.json file. Of particular relevance is the "python.testing.pytestArgs" setting.

    Another setting to try is "python.testing.cwd". Set it to the root directory from which you want to run your tests. Normally this is the workspace root.

    Finally, what happens when you run pytest manually in a terminal? What about if you run the same command that shows up in the "Python" output panel? The command output will be helpful.

  5. mikofski commented on Feb 17, 2021

    @mikofski
    Author

    Thank you very much!!!

    Here is the Python test log output:

    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
    

    I think my settings.json file is like this, it was auto generated:

    {
        "python.testing.pytestArgs": [],
        "python.testing.unittestEnabled": false,
        "python.testing.nosetestsEnabled": false,
        "python.testing.pytestEnabled": true,
        "python.pythonPath": "venv\\Scripts\\python.exe"
    }
    

    Pytest works fine when I run it manually in a terminal or from the vscode Python output. And discover tests works and tests run in debug. It's only the green play button on the GUI that raises the exception.

    I'm away from my laptop, but I will try setting python.testing.cwd asap. Thanks!

  6. ghost removed on Feb 17, 2021
  7. ghost added
    triage-neededNeeds assignment to the proper sub-team
    on Feb 17, 2021
  8. 5 remaining items

  9. ghost added
    triage-neededNeeds assignment to the proper sub-team
    on Feb 23, 2021
  10. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on Feb 23, 2021
  11. wholmgren commented on Feb 23, 2021

    @wholmgren

    Mark Mikofski (@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.

  12. wholmgren commented on Feb 23, 2021

    @wholmgren

    The relationship to installation issues and a desire to avoid real work led me try installing the package with pyproject.toml and moving the pytest config to that file too. Same issue. Branch here.

  13. mikofski commented on Feb 25, 2021

    @mikofski
    Author

    Will Holmgren (@wholmgren) I found a sneaky but unpleasant workaround. I insert this into pvlib/tests/conftest.py right before import pvlib:

    import sys
    sys.path.append(str(Path(__file__).parent.parent.parent))

    If I open a python interpreter and print the Python path the outcome of the "editable" install is that pvlib-python is last in the list:

    >>> import pprint
    >>> pprint.pprint(sys.path)
    ['',
     '/usr/lib/python38.zip',
     '/usr/lib/python3.8',
     '/usr/lib/python3.8/lib-dynload',
     '/home/mikm/Projects/pvlib-python/venv/lib/python3.8/site-packages',
     '/home/mikm/Projects/pvlib-python']
    

    but printing out the Python path from inside pvlib/tests/conftest.py:

    import pprint
    pprint.pprint(sys.path)

    shows during test execution it's been removed:

    ['/home/mikm/Projects/pvlib-python/pvlib/tests',
     '/home/mikm/.vscode-server/extensions/ms-python.python-2021.2.582707922/pythonFiles',
     '/usr/lib/python38.zip',
     '/usr/lib/python3.8',
     '/usr/lib/python3.8/lib-dynload',
     '/home/mikm/Projects/pvlib-python/venv/lib/python3.8/site-packages']
    
  14. mhostetter commented on Mar 4, 2021

    @mhostetter

    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.

  15. mikofski commented on Mar 31, 2021

    @mikofski
    Author

    Will Holmgren (@wholmgren) FYI vscode testing is working in other projects, so there is something specific about pvlib that is different.

    • conftest.py
    • tests/ doesn't have __init__.py
    • tests/ isn't at the project level, it's inside the pvlib package

    Have you tried the vscode insiders build and setting "python.useIsolation": false in the User Settings? This supposedly fixes the issue, but either I did it wrong or it didn't work for me. I guess setting python.useIsolation to false undoes this line:

    sys.path[:] = (p for p in sys.path if p != "" and normalize(p) != cwd)

    I guess I'm not terribly concerned b/c pytest works in a terminal and that's good enough for me. And visual debug still works in code, so that's nice. I'm kinda hoping this isn't a larger issue, and it just works itself out in time. 😏

  16. mikofski commented on Mar 31, 2021

    @mikofski
    Author
  17. mikofski commented on Mar 31, 2021

    @mikofski
    Author

    related to discussion #15494

  18. FrancescoValerio commented on Apr 28, 2021

    @FrancescoValerio

    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)
    
  19. locked as resolved and limited conversation to collaborators on Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-testingbugIssue identified by VS Code Team member as probable buginfo-neededIssue requires more information from poster

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions