使用pylint扩展时VSCode中的FileNotFoundError

0 投票
1 回答
52 浏览
提问于 2025-04-14 16:12

最近我在VSCode中运行pylint时遇到了下面的错误信息。 当我在命令行中使用“pylint <file/to/python/file>”运行pylint时,一切都正常。 直到几周前,它一直运行得很好。系统没有明显的变化。

VSCode版本:1.87.2 Python版本:3.12.0和3.9.5 操作系统:Windows 10

2024-03-19 17:52:58.086 [info] Name: Pylint
2024-03-19 17:52:58.086 [info] Module: pylint
2024-03-19 17:52:58.086 [info] Python extension loading
2024-03-19 17:52:58.086 [info] Waiting for interpreter from Python extension.
2024-03-19 17:52:58.086 [info] No interpreter found from setting pylint.interpreter
2024-03-19 17:52:58.086 [info] Getting interpreter from ms-python.python extension for workspace c:\Users\test_user\AppData\Local\Programs\Microsoft VS Code
2024-03-19 17:52:58.086 [info] Python extension loaded
2024-03-19 17:52:58.086 [info] Interpreter from ms-python.python extension for c:\Users\test_user\AppData\Local\Programs\Microsoft VS Code: c:\Program Files\Python312\python.exe
2024-03-19 17:52:58.086 [info] Using cwd from `python.analysis.extraPaths`.
2024-03-19 17:52:58.087 [info] Server run command: c:\Program Files\Python312\python.exe c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\tool\lsp_server.py
2024-03-19 17:52:58.087 [info] Server: Start requested.
2024-03-19 17:52:59.402 [info] CWD Server: c:\Users\test_user\AppData\Local\Programs\Microsoft VS Code
2024-03-19 17:52:59.403 [info] Settings used to run Server:
[]

2024-03-19 17:52:59.403 [info] Global settings:
{
    "cwd": "${workspaceFolder}",
    "workspace": "C:\\Users\\test_user\\AppData\\Local\\Programs\\Microsoft VS Code",
    "args": [
        "--max-line-length=120"
    ],
    "severity": {
        "convention": "Information",
        "error": "Error",
        "fatal": "Error",
        "refactor": "Hint",
        "warning": "Warning",
        "info": "Information"
    },
    "path": [
        "--max-line-length=120"
    ],
    "ignorePatterns": [],
    "interpreter": [],
    "importStrategy": "useBundled",
    "showNotifications": "off",
    "extraPaths": []
}

2024-03-19 17:52:59.404 [info] sys.path used to run Server:
   c:\Users\test_user\AppData\Local\Programs\Microsoft VS Code
   c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\libs
   c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\tool
   c:\Program Files\Python312\python312.zip
   c:\Program Files\Python312\DLLs
   c:\Program Files\Python312\Lib
   c:\Program Files\Python312
   C:\Users\test_user\AppData\Roaming\Python\Python312\site-packages
   C:\Users\test_user\AppData\Roaming\Python\Python312\site-packages\win32
   C:\Users\test_user\AppData\Roaming\Python\Python312\site-packages\win32\lib
   C:\Users\test_user\AppData\Roaming\Python\Python312\site-packages\Pythonwin
   c:\Program Files\Python312\Lib\site-packages
2024-03-19 17:52:59.413 [info] --max-line-length=120 --version
2024-03-19 17:52:59.413 [info] CWD Server: C:\Users\test_user\AppData\Local\Programs\Microsoft VS Code
2024-03-19 17:52:59.425 [info] Error while detecting pylint version:
Traceback (most recent call last):
  File "c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\tool\lsp_server.py", line 520, in _log_version_info
    result = _run_tool(["--version"], settings)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\tool\lsp_server.py", line 821, in _run_tool
    result = utils.run_path(argv=argv, use_stdin=True, cwd=cwd, env=env)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\tool\lsp_utils.py", line 226, in run_path
    with subprocess.Popen(
         ^^^^^^^^^^^^^^^^^
  File "c:\Program Files\Python312\Lib\subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "c:\Program Files\Python312\Lib\subprocess.py", line 1538, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] The system cannot find the file specified

2024-03-19 17:52:59.435 [info] [Trace - 5:52:59 PM] Sending request 'textDocument/codeAction - (1)'.
2024-03-19 17:52:59.440 [info] c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\tool\lsp_server.py:103: DeprecationWarning: 'workspace.get_document' has been deprecated, use 'workspace.get_text_document' instead
  document = LSP_SERVER.workspace.get_document(params.text_document.uri)

2024-03-19 17:52:59.447 [info] [Trace - 5:52:59 PM] Received notification 'window/logMessage'.
2024-03-19 17:52:59.447 [info] --max-line-length=120 --reports=n --output-format=json --max-line-length=120 --from-stdin c:\Users\test_user\Desktop\Untitled-1.py
2024-03-19 17:52:59.447 [info] [Trace - 5:52:59 PM] Received notification 'window/logMessage'.
2024-03-19 17:52:59.447 [info] CWD Server: C:\Users\test_user\Desktop
2024-03-19 17:52:59.466 [info] [Trace - 5:52:59 PM] Received notification 'window/logMessage'.
2024-03-19 17:52:59.466 [info] [Error - 5:52:59 PM] Linting failed with error:
Traceback (most recent call last):
  File "c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\tool\lsp_server.py", line 144, in _linting_helper
    result = _run_tool_on_document(document, use_stdin=True, extra_args=extra_args)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\tool\lsp_server.py", line 736, in _run_tool_on_document
    result = utils.run_path(
             ^^^^^^^^^^^^^^^
  File "c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\tool\lsp_utils.py", line 226, in run_path
    with subprocess.Popen(
         ^^^^^^^^^^^^^^^^^
  File "c:\Program Files\Python312\Lib\subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "c:\Program Files\Python312\Lib\subprocess.py", line 1538, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] The system cannot find the file specified

2024-03-19 17:52:59.468 [info] [Trace - 5:52:59 PM] Received notification 'textDocument/publishDiagnostics'.
2024-03-19 17:52:59.476 [info] c:\Users\test_user\.vscode\extensions\ms-python.pylint-2023.10.1\bundled\tool\lsp_server.py:298: DeprecationWarning: 'workspace.get_document' has been deprecated, use 'workspace.get_text_document' instead
  document = LSP_SERVER.workspace.get_document(params.text_document.uri)

2024-03-19 17:52:59.476 [info] [Trace - 5:52:59 PM] Received response 'textDocument/codeAction - (1)' in 41ms.
2024-03-19 17:52:59.499 [info] [Trace - 5:52:59 PM] Sending request 'textDocument/codeAction - (2)'.
2024-03-19 17:52:59.502 [info] [Trace - 5:52:59 PM] Received response 'textDocument/codeAction - (2)' in 3ms.
2024-03-19 17:52:59.796 [info] [Trace - 5:52:59 PM] Sending request 'textDocument/codeAction - (3)'.
2024-03-19 17:52:59.817 [info] [Trace - 5:52:59 PM] Received response 'textDocument/codeAction - (3)' in 21ms.

任何建议都非常感谢,谢谢

  • 我查看了pylint的GitHub仓库和StackOverflow,没找到相关的问题
  • 重新安装pylint扩展也没有帮助
  • 删除VSCode工作区也没有效果

1 个回答

0

解决方案

用户设置的json文件里有这么一个设置:
"pylint.path": ["--max-line-length=120"]
这个设置引起了问题,因为它改变了pylint的路径。

在日志中也能看到这个问题:
2024-03-19 17:52:59.413 [info] --max-line-length=120 --version
正常情况下应该是这样的:
2024-03-20 23:11:02.400 [info] c:\Program Files\Python312\python.exe -m pylint --version

如果其他人也需要分析类似的问题,我的处理方法是:

  • 重新安装了pylint扩展 -> 没有效果
  • 删除了C:\Users\test_user\AppData\Roaming\Python\Python312,因为里面几乎是空的(在另一台电脑上没有这个文件夹) -> 没有效果
  • 用系统安装程序重新安装了VSCode,而不是用户安装程序 -> 没有效果
  • 检查了我的用户settings.json文件中与pylint相关的设置(在最初的日志中可以看到)
    • "pylint.args": ["--max-line-length=120"] -> 删除了 -> 没有效果
    • "pylint.path": ["--max-line-length=120"] -> 删除了 -> 找到了根本原因

撰写回答