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

使用Node.js通过浏览器运行Python脚本时出现错误22求助

Fixing Error 22 When Running Python Script via Node.js PythonShell

Error 22 in PythonShell almost always ties back to a path-related problem—either your script path is malformed, the Python file doesn’t exist where you’re pointing, or Windows is misinterpreting the path syntax. Let’s break down the most likely fixes:

1. Fix Windows Path Escaping

In JavaScript string literals, backslashes (\) act as escape characters. Your current script path uses unescaped backslashes, which can mangle the path (this is why your error message shows a truncated D:\L4Project\wor...).

Update your scriptPath using one of these valid formats:

Option A: Double Backslashes (Windows-Specific)

var options = { scriptPath: 'D:\\L4Project\\working code\\finalproject\\routes' };

Option B: Forward Slashes (Cross-Platform Friendly)

var options = { scriptPath: 'D:/L4Project/working code/finalproject/routes' };

2. Verify the Script Exists at the Path

Double-check that sensitive2.py is actually located in D:\L4Project\working code\finalproject\routes. Typos (like capitalization errors, missing spaces, or incorrect subfolders) are super common here—even a tiny mistake will trigger error 22.

3. Get Full Error Details

The truncated error message isn’t giving you the whole picture. Modify your error handler to log the complete error object:

PythonShell.run('sensitive2.py', options, function (err, results) {
  if (err) {
    console.error('Full error info:', err);
    return; // Skip throwing to see the full log
  }
  console.log('results: %j', results);
});

This will show you the exact path PythonShell is trying to access, making it easier to spot where things went wrong.

4. Ensure Python is Accessible

If Python isn’t in your system’s PATH variable, specify the full path to your Python executable in the options:

var options = {
  scriptPath: 'D:/L4Project/working code/finalproject/routes',
  pythonPath: 'C:/Python311/python.exe' // Replace with your actual Python path
};

5. Check Permissions

Make sure the user running your Node.js process has read access to the routes directory and sensitive2.py file. On Windows, this might mean adjusting folder permissions if the directory is in a restricted location (like Program Files).

After trying these steps, your script should be able to locate and run the Python file without hitting error 22.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 11:01:59