Skip to content

ModuleNotFoundError and can't run/debug unittest #17363

Closed

Description

Environment data

  • VS Code version: 1.60.0
  • Extension version (available under the Extensions sidebar): 2021.9.1218897484
  • OS and version: MacOS Mojave 10.14.6 (in a Codespace)
  • Python version (& distribution if applicable, e.g. Anaconda): 3.9.6 64-bit
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): pipenv --global
  • Relevant/affected Python packages and their versions: N/A
  • Relevant/affected Python-related VS Code extensions and their versions: N/A
  • Value of the python.languageServer setting: not set

Expected behaviour

unittest tests are automatically detected and show up in testing side bar"

Screen Shot 2021-09-09 at 19 25 42

Actual behaviour

They do not, and I get:

Screen Shot 2021-09-09 at 19 21 59

Steps to reproduce:

While working on https://github.com/JEFuller/dataclasses-configobj

Running on 2021.9.1191016588 is ✅
but updating to 2021.9.1218897484 is ❌

I can switch between versions and the tests appear and disappear, as show above ⬆️

Logs

Output for Python in the Output panel >

/usr/bin/python3 ~/.vscode-remote/extensions/ms-python.python-2021.9.1218897484/pythonFiles/testing_tools/unittest_discovery.py ./tests test_*.py
cwd: /workspaces/dataclasses-configobj
Error 2021-09-10 02:00:49: Error discovering unittest tests:
 Failed to import test module: test_core
Traceback (most recent call last):
  File "/usr/lib/python3.9/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.9/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/workspaces/dataclasses-configobj/tests/test_core.py", line 7, in <module>
    from dataclasses_configobj import core
ModuleNotFoundError: No module named 'dataclasses_configobj'

Activity

  1. added
    bugIssue identified by VS Code Team member as probable bug
    triage-neededNeeds assignment to the proper sub-team
    on Sep 10, 2021
  2. hogegex commented on Sep 10, 2021

    @hogegex

    I have the same problem.
    It seems that the directories inside the workspace folder are not recognized as packages.
    (PYTHONPATH problem?)

    If write "import my_pkg" to test_foo.py of the following files, the test will not be detected.
    If remove it, the test will be detected.

    + <workspaceFolder>
      + my_pkg
        - __init__.py
        - some.py
      + tests
        - test_foo.py
    
  3. karthiknadig commented on Sep 10, 2021

    @karthiknadig
    Member

    Looks like the issue is with PYTHONPATH. essentially when we were running discovery using '-c' Python would add [''] to the front of sys.path, this meant that the CWD would be looked at while import resolution. But when we switched to using a script, it does not add [''], which cause the module not found error.

    Brett Cannon (@brettcannon) What do you recommend we do here? insert '' to sys.path in the unittest discovery script, OR is there a better way?

  4. helloybz commented on Sep 10, 2021

    @helloybz

    I have the same problem.

    Below is my unittest configures in .vscode/setting.json.

    {
    "python.testing.unittestArgs": [
            "-v",
            "-s",
            "./tests",
            "-p",
            "test_*.py"
        ]
    }

    And I've found that the start-directory "./test" becomes "/test" in discover::unittest.loader.TestLoader

    Any idea about why the "./test" becomes "/test"??

  5. karthiknadig commented on Sep 10, 2021

    @karthiknadig
    Member

    Youngbeom Choi (@helloybz) Can you make sure that you are using latest python extension version: 2021.9.1218897484?
    With the settings you mentioned above I am seeing this, I don't see it missing the .:
    image

  6. brettcannon commented on Sep 10, 2021

    @brettcannon
    Member

    The cwd should still be the first entry in sys.path regardless of whether -m or -c was used.

    # In site-packages named `sys_path`.
    import sys
    
    if __name__ == '__main__':
        print(sys.path)
    ❯ ./test-venv/bin/python -m sys_path
    ['/tmp', '/home/linuxbrew/.linuxbrew/opt/python@3.9/lib/python39.zip', '/home/linuxbrew/.linuxbrew/opt/python@3.9/lib/python3.9', '/home/linuxbrew/.linuxbrew/opt/python@3.9/lib/python3.9/lib-dynload', '/tmp/test-venv/lib/python3.9/site-packages']
    
  7. karthiknadig commented on Sep 10, 2021

    @karthiknadig
    Member

    This is using script, so no -m or -c.

  8. tonycassara commented on Sep 30, 2021

    @tonycassara

    Hey Karthik Nadig (@karthiknadig) was there a resolution for this? I'm having the exact same issue, latest Python extension installed v2021.9.1246542782

  9. karthiknadig commented on Sep 30, 2021

    @karthiknadig
    Member

    This particular issue is fixed. If you are seeing ModuleNotFoundError it can be for a variety of reasons. Please file a separate bug with details so we can investigate.

  10. LoicH commented on Oct 5, 2021

    @LoicH

    I was having the same issue, stumbled upon this page, and for those who still have the same problem: don't forget to run pip install to have your requirements to run your tests 😉

  11. dlencer commented on Oct 7, 2021

    @dlencer

    I still have this issue with 2021.9.1246542782. Manually, I can run the command that VS Code seems to execute, python ~/.vscode-server/extensions/ms-python.python-2021.9.1246542782/pythonFiles/testing_tools/unittest_discovery.py ./tests test_*.py inside my venv just fine, but VS Code does not find the tests.

  12. dlencer commented on Oct 8, 2021

    @dlencer

    With today's update (VS Code 1.61, Python Extension v2021.10.1317843341), test discovery is working again. Thanks!

  13. locked as resolved and limited conversation to collaborators on Nov 30, 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 bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions