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

Cannot Debug Pytests: AttributeError: module 'io' has no attribute 'text_encoding' #23776

Closed
schwaerz opened this issue Jul 9, 2024 · 4 comments
Assignees
Labels
area-testing triage-needed Needs assignment to the proper sub-team

Comments

@schwaerz
Copy link

schwaerz commented Jul 9, 2024

Type: Bug

Behaviour

I set up a small python project (using pdm & pytest). I can run all the unit tests from within vscode using the TESTING tab. However when I try to debug a test, I will get the following error message on the TEST_RESULTS tab:

Error while running tests: test_conan_info_version_foo
TypeError: Cannot read properties of null (reading 'testsuites')

Error while running tests:
TypeError: Cannot read properties of null (reading 'testsuites')

I am using Debian Bookworm in WSL and running VSCode on Win11.

Steps to reproduce:

  1. Create new folder foo in your home directory in WSL (I tried using Debian Bookworm)
  2. cd foo
  3. run pdm init
  4. run pdm add pytest
  5. Create new file tests/foo_test.py. Content:
    def test_foo():
        assert False
  6. Enable the vscode testing, choose: pytest, choost tests folder as test folder
  7. Run the test once. It should fail with assert False.
  8. Set a breakpoint in line 2 of foo_test.py
  9. Right-click on the red cross in line 1 of foo_test.py and choose Debug Test

The error will occur as described.

Diagnostic data

Debug console shows:

Traceback (most recent call last):
  File "/usr/lib/python3.11/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/schwaerz/.vscode-server/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/home/schwaerz/.vscode-server/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/home/schwaerz/.vscode-server/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/home/schwaerz/.vscode-server/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/schwaerz/.vscode-server/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/schwaerz/.vscode-server/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "/home/schwaerz/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/testlauncher.py", line 44, in <module>
    run(cwd, testRunner, args)
  File "/home/schwaerz/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/testlauncher.py", line 36, in run
    pytest.main(args)
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/_pytest/config/__init__.py", line 159, in main
    config = _prepareconfig(args, plugins)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/_pytest/config/__init__.py", line 346, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/pluggy/_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 139, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 122, in _multicall
    teardown.throw(exception)  # type: ignore[union-attr]
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/_pytest/helpconfig.py", line 106, in pytest_cmdline_parse
    config = yield
             ^^^^^
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/_pytest/config/__init__.py", line 1152, in pytest_cmdline_parse
    self.parse(args)
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/_pytest/config/__init__.py", line 1501, in parse
    self._preparse(args, addopts=addopts)
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/_pytest/config/__init__.py", line 1373, in _preparse
    self._initini(args)
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/_pytest/config/__init__.py", line 1247, in _initini
    rootpath, inipath, inicfg = determine_setup(
                                ^^^^^^^^^^^^^^^^
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/_pytest/config/findpaths.py", line 201, in determine_setup
    rootdir, inipath, inicfg = locate_config(invocation_dir, [ancestor])
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/_pytest/config/findpaths.py", line 113, in locate_config
    ini_config = load_config_dict_from_file(p)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/schwaerz/repositories/foo/.venv/lib/python3.11/site-packages/_pytest/config/findpaths.py", line 69, in load_config_dict_from_file
    toml_text = filepath.read_text(encoding="utf-8")
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/pathlib.py", line 1058, in read_text
    encoding = io.text_encoding(encoding)
               ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/io.py", line 73, in __getattr__
    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
AttributeError: module 'io' has no attribute 'text_encoding'
Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

2024-07-09 18:53:38.186 [info] Environments refresh finished (event): 3632 milliseconds
2024-07-09 18:53:38.186 [info] Environments refresh finished (event): 2425 milliseconds
2024-07-09 18:53:38.188 [info] Environment refresh took 3635 milliseconds
2024-07-09 18:53:38.189 [info] Environment refresh took 2428 milliseconds
2024-07-09 18:53:38.191 [info] > pyenv which python
2024-07-09 18:53:38.191 [info] cwd: .
2024-07-09 18:53:38.346 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:53:38.350 [info] Python interpreter path: ./.venv/bin/python
2024-07-09 18:53:39.781 [info] Starting Pylance language server.
2024-07-09 18:54:36.084 [info] Discover tests for workspace name: foo - uri: /home/schwaerz/repositories/foo/tests/test.py
2024-07-09 18:54:39.281 [info] Discover tests for workspace name: foo - uri: /home/schwaerz/repositories/foo/tests/test.py
2024-07-09 18:54:42.606 [info] Discover tests for workspace name: foo - uri: /home/schwaerz/repositories/foo
2024-07-09 18:54:46.258 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:54:46.365 [info] Send text to terminal: source /home/schwaerz/repositories/foo/.venv/bin/activate
2024-07-09 18:54:50.228 [info] Discover tests for workspace name: foo - uri: /home/schwaerz/repositories/foo
2024-07-09 18:54:54.120 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:54:54.131 [info] > . ./.venv/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/printEnvVariables.py
2024-07-09 18:54:54.131 [info] shell: bash
2024-07-09 18:54:54.166 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:54:54.170 [info] > ./.venv/bin/python -c "import pytest"
2024-07-09 18:54:54.977 [info] Discover tests for workspace name: foo - uri: /home/schwaerz/repositories/foo/.vscode/settings.json
2024-07-09 18:54:55.354 [info] Discover tests for workspace name: foo - uri: /home/schwaerz/repositories/foo/.vscode/settings.json
2024-07-09 18:54:55.699 [info] Discover tests for workspace name: foo - uri: /home/schwaerz/repositories/foo/.vscode/settings.json
2024-07-09 18:54:55.700 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:54:55.712 [info] > . ./.venv/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/printEnvVariables.py
2024-07-09 18:54:55.712 [info] shell: bash
2024-07-09 18:54:55.746 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:54:55.750 [info] > ./.venv/bin/python ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/testing_tools/run_adapter.py discover pytest -- --rootdir . -s --cache-clear tests
2024-07-09 18:54:55.751 [info] cwd: .
2024-07-09 18:54:56.232 [info] Discover tests for workspace name: foo - uri: /home/schwaerz/repositories/foo
2024-07-09 18:54:56.232 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:54:56.235 [info] > ./.venv/bin/python ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/testing_tools/run_adapter.py discover pytest -- --rootdir . -s --cache-clear tests
2024-07-09 18:54:56.235 [info] cwd: .
2024-07-09 18:55:01.789 [info] Discover tests for workspace name: foo - uri: /home/schwaerz/repositories/foo
2024-07-09 18:55:01.790 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:55:01.793 [info] > ./.venv/bin/python ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/testing_tools/run_adapter.py discover pytest -- --rootdir . -s --cache-clear tests
2024-07-09 18:55:01.793 [info] cwd: .
2024-07-09 18:55:05.149 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:55:05.152 [info] > ./.venv/bin/python -c "import pytest"
2024-07-09 18:55:21.248 [info] Discover tests for workspace name: foo - uri: /home/schwaerz/repositories/foo
2024-07-09 18:55:21.248 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:55:21.252 [info] > ./.venv/bin/python ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/testing_tools/run_adapter.py discover pytest -- --rootdir . -s --cache-clear tests
2024-07-09 18:55:21.252 [info] cwd: .
2024-07-09 18:56:49.174 [warning] could not find a pixi interpreter for the interpreter at /home/schwaerz/repositories/foo/.venv/bin/python
2024-07-09 18:56:49.178 [info] > ./.venv/bin/python -m pytest --rootdir . --override-ini junit_family=xunit1 --junit-xml=/tmp/tmp-16777oWev4uGdPPR5.xml ./tests/foo_test.py::test_foo
2024-07-09 18:56:49.178 [info] cwd: .
2024-07-09 18:58:40.056 [info] Using launch configuration in workspace folder.
2024-07-09 18:58:40.247 [info] CreateEnv Trigger - Skipping for /home/schwaerz/repositories/foo

Extension version: 2024.10.0
VS Code version: Code 1.91.0 (ea1445cc7016315d0f5728f8e8b12a45dc0a7286, 2024-07-01T18:52:22.949Z)
OS version: Windows_NT x64 10.0.22631
Modes:
Remote OS version: Linux x64 5.15.123.1-microsoft-standard-WSL2

  • Python version (& distribution if applicable, e.g. Anaconda): 3.11.2
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv
  • Value of the python.languageServer setting: Pylance
User Settings


languageServer: "Pylance"

testing
• pytestArgs: "<placeholder>"
• pytestEnabled: true

Installed Extensions
Extension Name Extension Id Version
Behave VSC jimasp.behave-vsc 0.6.4
C/C++ ms-vscode.cpptools 1.20.5
C/C++ Extension Pack ms-vscode.cpptools-extension-pack 1.3.0
CMake twxs.cmake 0.0.17
CMake Tools ms-vscode.cmake-tools 1.18.42
Cucumber (Gherkin) Full Support alexkrechik.cucumberautocomplete 3.0.5
Docker ms-azuretools.vscode-docker 1.29.1
Even Better TOML tamasfe.even-better-toml 0.19.2
Git Extension Pack donjayamanne.git-extension-pack 0.1.3
Git Graph mhutchie.git-graph 1.30.0
Git History donjayamanne.githistory 0.6.20
gitignore codezombiech.gitignore 0.9.0
GitLens — Git supercharged eamodio.gitlens 15.1.0
JavaScript Debugger ms-vscode.js-debug 1.91.0
Open in GitHub, Bitbucket, Gitlab, VisualStudio.com ! ziyasal.vscode-open-in-github 1.3.6
Pylance ms-python.vscode-pylance 2024.7.1
Pytest Runner for Visual Studio Code pamaron.pytest-runner 0.0.7
Python ms-python.python 2024.10.0
Python Debugger ms-python.debugpy 2024.6.0
Ruff charliermarsh.ruff 2024.30.0
Table Visualizer for JavaScript Profiles ms-vscode.vscode-js-profile-table 1.0.9
vscode-reveal evilz.vscode-reveal 4.3.3
System Info
Item Value
CPUs 12th Gen Intel(R) Core(TM) i7-12800H (20 x 2803)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) undefined
Memory (System) 31.60GB (8.53GB free)
Process Argv
Screen Reader no
VM 0%
Item Value
Remote WSL: Debian
OS Linux x64 5.15.123.1-microsoft-standard-WSL2
CPUs 12th Gen Intel(R) Core(TM) i7-12800H (20 x 0)
Memory (System) 15.42GB (14.01GB free)
VM 0%
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Jul 9, 2024
@eleanorjboyd
Copy link
Member

Hi! What pytest version are you using?

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Jul 9, 2024
@eleanorjboyd
Copy link
Member

Hi! It looks like you are using the python extension without the testing rewrite which should fix your issue. Please opt in and give it another try. You can opt in by adding the following to your user settings: "python.experiments.optInto": ["pythonTestAdapter"],

@schwaerz
Copy link
Author

After opting in, the debugging works :)

Thanks 👍

Hope I will not face any other issues by opting in. Will this get productive soon? I think to remember that this used to work w/o this.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Jul 10, 2024
@eleanorjboyd
Copy link
Member

The experiment is turned on for 100% of users and we will take it out of experiment and make it the only option soon!

@eleanorjboyd eleanorjboyd closed this as not planned Won't fix, can't repro, duplicate, stale Jul 10, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

2 participants