最近遇到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
嫌方法一麻烦直接安装个node.js
吧,一劳永逸。
写在最后
两个字:蛋疼