microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.34k stars 1.19k forks source link

ImportError: Error importing plugin "vscode_pytest": No module named 'vscode_pytest' #24414

Open talebia opened 3 days ago

talebia commented 3 days ago

similar to 23192 I'm using python 3.10.10 and below are the logs:

2024-11-11 12:17:09.161 [debug] Testing: Manually triggered test refresh
2024-11-11 12:17:09.161 [debug] Testing: Clearing all discovered tests
2024-11-11 12:17:09.161 [debug] Testing: Forcing test data refresh
2024-11-11 12:17:09.161 [debug] Testing: Refreshing all test data
2024-11-11 12:17:09.161 [debug] Found cached env for C:\Python\python.exe
2024-11-11 12:17:09.162 [info] Discover tests for workspace name: compute.rhino3d - uri: c:\dev\MyProject\MySolution
2024-11-11 12:17:09.162 [info] Running discovery for pytest using the new test adapter.
2024-11-11 12:17:09.162 [debug] Found cached env for C:\Python\python.exe
2024-11-11 12:17:09.163 [debug] Starting Test Discovery named pipe
2024-11-11 12:17:09.163 [debug] Creating named pipe server on \\.\pipe\python-test-discovery-a96448a0ff04d34aa1ac-sock
2024-11-11 12:17:09.165 [warning] The cwd resolves to a different path, checking if it has a symbolic link somewhere in its path.
2024-11-11 12:17:09.166 [info] All environment variables set for pytest discovery: {"ALLUSERSPROFILE":"C:\\ProgramData","APPDATA":"C:\\Users\\USER.NAME\\AppData\\Roaming","TEST_RUN_PIPE":"\\\\.\\pipe\\python-test-discovery-a96448a0ff04d34aa1ac-sock"}
2024-11-11 12:17:09.166 [debug] could not find a pixi interpreter for the interpreter at C:\Python\python.exe
2024-11-11 12:17:09.207 [debug] Activation Commands received undefined for shell cmd, resource c:\dev\MyProject\MySolution and interpreter C:\Python\python.exe
2024-11-11 12:17:09.219 [debug] Running pytest discovery with command: -m pytest -p vscode_pytest --collect-only myco_tests/bdd_tests for workspace c:\dev\MyProject\MySolution.
2024-11-11 12:17:09.219 [info] > C:\Python\python.exe -m pytest -p vscode_pytest --collect-only myco_tests/bdd_tests
2024-11-11 12:17:09.219 [info] cwd: .
2024-11-11 12:17:09.746 [error] Traceback (most recent call last):
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 864, in import_plugin
    __import__(importspec)
ModuleNotFoundError: No module named 'vscode_pytest'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "runpy.py", line 196, in _run_module_as_main
  File "runpy.py", line 86, in _run_code
  File "C:\Python\lib\site-packages\pytest\__main__.py", line 7, in <module>
    raise SystemExit(pytest.console_main())
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 206, in console_main
    code = main()
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 159, in main
    config = _prepareconfig(args, plugins)
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 346, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
  File "C:\Python\lib\site-packages\pluggy\_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "C:\Python\lib\site-packages\pluggy\_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "C:\Python\lib\site-packages\pluggy\_callers.py", line 139, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "C:\Python\lib\site-packages\pluggy\_callers.py", line 122, in _multicall
    teardown.throw(exception)  # type: ignore[union-attr]
  File "C:\Python\lib\site-packages\_pytest\helpconfig.py", line 106, in pytest_cmdline_parse
    config = yield
  File "C:\Python\lib\site-packages\pluggy\_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 1151, in pytest_cmdline_parse
    self.parse(args)
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 1500, in parse
    self._preparse(args, addopts=addopts)
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 1383, in _preparse
    self.pluginmanager.consider_preparse(args, exclude_only=False)
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 794, in consider_preparse
    self.consider_pluginarg(parg)
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 817, in consider_pluginarg
    self.import_plugin(arg, consider_entry_points=True)
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 866, in import_plugin
    raise ImportError(
  File "C:\Python\lib\site-packages\_pytest\config\__init__.py", line 864, in import_plugin
    __import__(importspec)
ImportError: Error importing plugin "vscode_pytest": No module named 'vscode_pytest'

2024-11-11 12:17:09.769 [error] Subprocess exited unsuccessfully with exit code 1 and signal null on workspace c:\dev\MyProject\MySolution.
2024-11-11 12:17:09.769 [error] Subprocess exited unsuccessfully with exit code 1 and signal null on workspace c:\dev\MyProject\MySolution. Creating and sending error discovery payload
2024-11-11 12:17:09.769 [error] pytest test discovery error for workspace:  c:\dev\MyProject\MySolution 

 The python test process was terminated before it could exit on its own, the process errored with: Code: 1, Signal: null for workspace c:\dev\MyProject\MySolution
2024-11-11 12:25:13.769 [debug] Found cached env for C:\Python\python.exe
2024-11-11 12:25:13.769 [debug] Found cached env for C:\Python\python.exe
2024-11-11 12:29:49.793 [debug] Found cached env for C:\Python\python.exe
2024-11-11 12:29:54.937 [debug] Received shell integration sequence for powershell
2024-11-11 12:30:00.154 [debug] Found cached env for C:\Python\python.exe
2024-11-11 12:30:00.154 [debug] Selected python for [c:\dev\MyProject\MySolution] is [global] type: C:\Python\python.exe
2024-11-11 12:30:00.178 [debug] Extension unknown accessed getActiveEnvironmentPath with args: undefined
2024-11-11 12:30:00.178 [debug] Extension unknown accessed resolveEnvironment with args: {"id":"C:\\PYTHON\\PYTHON.EXE","path":"C:\\Python\\python.exe"}
2024-11-11 12:30:05.043 [debug] Received shell integration sequence for powershell
2024-11-11 12:30:11.862 [debug] Found cached env for C:\Python\python.exe
2024-11-11 12:30:11.863 [debug] Selected python for [c:\dev\MyProject\MySolution] is [global] type: C:\Python\python.exe
2024-11-11 12:30:11.893 [debug] Extension unknown accessed resolveEnvironment with args: {"id":"C:\\PYTHON\\PYTHON.EXE","path":"C:\\Python\\python.exe"}
2024-11-11 12:30:11.893 [debug] Extension unknown accessed getActiveEnvironmentPath with args: undefined
2024-11-11 12:30:11.919 [debug] Received shell integration sequence for powershell
2024-11-11 12:31:14.693 [debug] Found cached env for C:\Python\python.exe

Similarly can get around it by using something like this but not sustainable.

karthiknadig commented 3 days ago

@talebia Can you check if the vscode_pytest exists in the extensions directory? You can find the extensions directory by running this command: Image

Then open the folder that starts with ms-python.python. In that directory there should be python_files directory, open it. There you should find vscode_pytest: Image

Image

Image

Make sure that this folder exists. If it does not exit, try uninstalling python extension, close all vscode instances, and reopen vs code and install python extension again.

talebia commented 3 days ago

@karthiknadig I do seem to have it although slightly different version that above: Image

karthiknadig commented 3 days ago

That looks correct. In the logs you pasted, it looks like the environment variables were truncated, can you make sure it has PYTHONPATH set to: Image

talebia commented 3 days ago

I can set the env var and it works fine. though would I have to update it manually each time there's a new version? plus I'm not sure why it was not set in the first place..

karthiknadig commented 3 days ago

@talebia You should not have to set this, it should set automatically. It looked like in the logs only TEST_RUN_PIPE got set, the PYTHONPATH did not. This might be a bug.

eleanorjboyd commented 2 days ago

Thanks for all the info above, very weird the env vars are not getting set correctly. @talebia could you describe your setup? ie are you on a remote machine, what type of machine you have, where is this folder you are running test discovery on located. I am seeing in your logs 2024-11-11 12:17:09.165 [warning] The cwd resolves to a different path, checking if it has a symbolic link somewhere in its path. which means the realpath of the cwd is not the cwd (it evaluates to something else) but it does not find a symlink in the cwd so I am trying to think what scenario it might be and if thats related

eleanorjboyd commented 2 days ago

One thing to try that just came to mind- could you disable pytest and enable unittest instead and trying running discovery that way? To do so update your settings.json for your workspace:

    "python.testing.unittestArgs": ["-v", "-s", ".", "-p", "*test*.py"],
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": true

now RELOAD the VS Code window (important step to make sure the setting gets updated correctly)

then attempt to discover a unittest, which you can use this example if you don't have one.

import unittest

class TestExample(unittest.TestCase):
    def test_true_unittest(self):
        assert True

see how that goes as unittest uses the same env var substitution but with some small changes