You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode Testing扩展调试Python pytest用例报错“Unable to read file”求助

Solutions for "Unable to read file" When Debugging pytest via VSCode Testing Extension

Let’s work through this step by step—since you’ve already tried the common fix of deleting the .vscode folder, let’s dig into other likely causes tailored to your Python project:

  • First, validate the problematic test cases directly
    Sometimes the issue isn’t the extension itself, but broken syntax, invalid imports, or malformed test functions in those specific cases. Run just those tests in your terminal with pytest to rule out code-level issues:

    pytest path/to/your/test/file.py::specific_test_function_name -xvs
    

    If pytest throws an error here, fix the syntax/imports in those tests first, and the extension should start working again.

  • Double-check your VSCode workspace and file paths
    Make sure you’ve opened the root folder of your project in VSCode, not a subfolder. If the test file lives in a path with special characters (like spaces, accented letters, or non-ASCII symbols), that can confuse the extension. Try renaming the file/folder to use only alphanumeric characters and underscores if needed.

  • Reset the Testing Extension and your Python environment

    1. Open the command palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac) and run Developer: Reload Window to fully refresh VSCode.
    2. If that doesn’t help, go to the Extensions panel, find the Testing extension, disable it, restart VSCode, then re-enable it. Uninstalling and reinstalling the extension can also clear stubborn cached issues.
    3. Confirm your Python interpreter: Click the Python version in the bottom-left corner of VSCode, and ensure you’re using the virtual environment tied to your project (where pytest is installed). The extension sometimes defaults to the global Python instead of your project’s venv, causing path mismatches.
  • Inspect your VSCode pytest settings for misconfiguration
    Even after deleting the .vscode folder, VSCode might retain cached workspace settings. Open your settings (Ctrl+,) and search for python.testing.pytestArgs. Make sure the arguments don’t include invalid paths or accidentally exclude the problematic test file. For example, a line like ["--ignore=path/to/your/test/file.py"] would block the extension from reading it.

  • Isolate the test file to rule out corruption
    Copy the problematic test cases into a brand new Python file in your project, then try debugging that new file via the Testing Extension. If it works, the original file might have hidden corruption (like invisible control characters). You can then copy the fixed tests back to the original file.

  • Check file permissions (Linux/macOS only)
    If you’re on a Unix-based system, ensure the test file has read permissions. Run this in your terminal:

    chmod +r path/to/your/test/file.py
    

These steps should cover most edge cases that the standard .vscode reset doesn’t address. Let me know if any of these work, or if you need more details!

内容的提问来源于stack exchange,提问作者William

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:27:32