信息: 用提供的模式无法找到文件。问题解决

在这里插入图片描述

最近遇到python项目运行就提示信息: 用提供的模式无法找到文件。的问题。虽然对程序来说没什么影响,但是强迫症的我看着很不爽。

问题

问题出在Gradio5.X版本需要找到本机的nodejs安装路径。我尝试用Gradio4.X版本没有这个情况。

找了很久,最后是site-packages\gradio\utils.py脚本中的这个get_node_path函数报错。如果没有GRADIO_NODE_PATH这个环境变量就会使用where命令去查询node,如果本机没安装nodejs的话就会提示信息: 用提供的模式无法找到文件。

def get_node_path():
    env_node_path = os.environ.get("GRADIO_NODE_PATH")
    if env_node_path:
        return env_node_path

    which_node_path = shutil.which("node")
    if which_node_path:
        return which_node_path

    try:
        # On Windows, try using 'where' command
        if sys.platform == "win32":
            windows_path = (
                subprocess.check_output(["where", "node"])
                .decode()
                .strip()
                .split("\r\n")[0]
            )
            # Verify the path exists
            if os.path.exists(windows_path):
                return windows_path
        # Try using the 'which' command on Unix-like systems
        else:
            return subprocess.check_output(["which", "node"]).decode().strip()

    except subprocess.CalledProcessError:
        # Command failed, fall back to checking common install locations
        pass

    # Check common install locations
    common_paths = [
        sys.executable,
        "/usr/bin/node",
        "/usr/local/bin/node",
        "C:\\Program Files\\nodejs\\node.exe",
        "C:\\Program Files (x86)\\nodejs\\node.exe",
    ]

    for node_path in common_paths:
        if os.path.exists(node_path):
            return node_path

    # Check PATH environment variable
    env_path = os.environ.get("PATH", "")
    path_dirs = env_path.split(os.pathsep)

    for directory in path_dirs:
        full_path = os.path.join(
            directory, "node.exe" if sys.platform == "win32" else "node"
        )
        if os.path.exists(full_path):
            return full_path

    print("Unable to find node install path, falling back to SPA mode.")
    print(
        "If you wish to use the node backend, please install node 20 and/ or set the path with the GRADIO_NODE_PATH environment variable."
    )
    return None

解决方法

方法一:

设置GRADIO_NODE_PATH环境变量

set GRADIO_NODE_PATH=C:\Path\To\node.exe

每次都要设置,比较麻烦。

方法二:

安装Node.js

https://nodejs.org/zh-cn

嫌方法一麻烦直接安装个node.js吧,一劳永逸。

写在最后

两个字:蛋疼

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值