Custom component dev server broke on windows

用户按照 Gradio 官方教程创建自定义 Chatbot 组件,在 Windows 系统的 conda 环境( AIenv )中执行 gradio cc create EditableChatbot --template Chatbot 创建项目后,运行 gradio cc dev 启动开发服务器

Custom component dev server broke on windows

Custom component dev server broke on windows

快速结论:在 Windows 上使用 conda 环境运行 gradio cc dev 开发自定义组件时,开发服务器可能无法正确识别 conda 环境中的可执行文件,导致后端提示找不到自定义组件且前端无法加载资源。优先排查 --gradio-path--python-path 参数是否正确传入。

问题场景

用户按照 Gradio 官方教程创建自定义 Chatbot 组件,在 Windows 系统的 conda 环境(AIenv)中执行 gradio cc create EditableChatbot --template Chatbot 创建项目后,运行 gradio cc dev 启动开发服务器。终端出现错误提示,前端服务器无法正确加载 index.jsstyle.css 文件。

报错原文

No custom components were found in D:\project\editablechatbot. It is likely that dev mode does not work properly. Please pass the --gradio-path
and --python-path CLI arguments so that gradio uses the right executables.

Backend Server:  http://127.0.0.1:7861

Failed to resolve dependency: [36msvelte/animate[39m, present in 'optimizeDeps.include'
Failed to resolve dependency: [36msvelte/easing[39m, present in 'optimizeDeps.include'
Failed to resolve dependency: [36msvelte/internal[39m, present in 'optimizeDeps.include'
...
Failed to resolve dependency: [36msvelte[39m, present in 'optimizeDeps.include'

Frontend Server (Go here): http://localhost:7862/

后续尝试传入路径参数后又出现新错误:

X [ERROR] Could not resolve "virtual:component-loader"

    frontend/node_modules/@gradio/utils/src/utils.ts:237:10:
      237 │       import("virtual:component-loader").then((module) => {
          ╵              ~~~~~~~~~~~~~~~~~~~~~~~~~~

[2m20:05:26[22m [31m[1m[22m[39m [31merror while updating dependencies:
Error: Build failed with 1 error:
frontend/node_modules/@gradio/utils/src/utils.ts:237:10: ERROR: Could not resolve "virtual:component-loader"
    at failureErrorWithLog (D:\project\editablechatbot\frontend\node_modules\vite\node_modules\esbuild\lib\main.js:1472:15)
    ...

原因分析

主要原因:gradio cc dev 命令未能找到 conda 虚拟环境 AIenv 中的正确可执行文件。在 Windows 上,直接运行 gradio cc dev 时,可能会调用系统默认或错误的 Python/gradio 路径,而不是 conda 环境中的版本,导致后端找不到自定义组件。

即使传入 --gradio-path--python-path 后,用户又遇到了 virtual:component-loader 解析失败的问题。该问题出现在前端构建环节,可能原因是 @gradio/preview 包的版本不兼容或缺少必要的 Vite 插件。

环境排查

  • 操作系统:Windows(用户环境为 Windows)
  • Gradio 版本:4.38.1(用户环境)
  • Python 虚拟环境:conda 环境 AIenv
  • 前端的 @gradio/preview 版本:0.10.1(用户确认安装)
  • 用户尝试过的环境:Windows(conda)和 Ubuntu 22 虚拟机(miniconda),问题均重现

解决步骤

  1. 优先尝试:在运行 gradio cc dev 时,显式传入 --gradio-path--python-path 参数,指定为 conda 环境中的实际路径。例如:
    gradio cc dev --gradio-path <your_conda_env_path>\Scripts\gradio.exe --python-path <your_conda_env_path>\python.exe
  2. 如果步骤 1 后仍然出现 virtual:component-loader 错误,检查 frontend/package.json 中的 @gradio/preview 版本是否为 0.10.1。如果不是,请确认组件模板的依赖版本与 Gradio 主版本兼容。
  3. 如果以上步骤仍无法解决问题(如用户反馈在 Ubuntu 上也遇到相同错误),你可以在 WSL(Windows Subsystem for Linux)上尝试开发,Issue 中有提到“It should work there”。

验证方法

运行 gradio cc dev(或带参数运行)后,观察终端输出:如果不出现 No custom components were found 以及 Could not resolve "virtual:component-loader" 错误,并且浏览器能正常打开前端地址(如 http://localhost:7862/)显示自定义组件,则表示问题已解决。

参考来源

gradio-app/gradio #8781

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 14755

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注