Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError and can't run/debug unittest #17363

Closed
davetapley opened this issue Sep 10, 2021 · 11 comments · Fixed by #17369
Closed

ModuleNotFoundError and can't run/debug unittest #17363

davetapley opened this issue Sep 10, 2021 · 11 comments · Fixed by #17369
Assignees
Labels
area-testing bug Issue identified by VS Code Team member as probable bug

Comments

@davetapley
Copy link

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'

@davetapley davetapley added bug Issue identified by VS Code Team member as probable bug triage-needed Needs assignment to the proper sub-team labels Sep 10, 2021
@hogegex
Copy link

hogegex commented Sep 10, 2021

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

@karthiknadig
Copy link
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.

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

@helloybz
Copy link

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"??

@karthiknadig
Copy link
Member

@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

@brettcannon
Copy link
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']

@karthiknadig
Copy link
Member

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

@tonycassara
Copy link

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

@karthiknadig
Copy link
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.

@LoicH
Copy link

LoicH commented Oct 5, 2021

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 😉

@dlencer
Copy link

dlencer commented Oct 7, 2021

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.

@dlencer
Copy link

dlencer commented Oct 8, 2021

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants