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

在Cloud Function中调用Python脚本遇文件不存在错误求助

解决Cloud Function中python-shell找不到脚本文件的问题

问题原因

你遇到的No such file or directory错误,核心问题是你指定的脚本路径是本地机器的私有路径:/home/dmproject0608/test2,而Cloud Functions运行在Google的托管容器环境中,这个本地路径在云端环境里完全不存在,自然找不到你的FaceDetect_.py脚本。

解决方案

要让Cloud Function能访问到Python脚本,你需要把脚本和Cloud Function的代码一起部署,然后使用云端环境能识别的相对路径。

步骤1:调整本地文件结构

把你的FaceDetect_.py脚本和Cloud Function的入口文件(比如index.js)放在同一个目录下,或者放在一个专门的子目录(比如scripts文件夹),确保部署时脚本会被打包上传到云端。

示例文件结构:

your-function-folder/
├── index.js
├── package.json
└── FaceDetect_.py  # 或者放在scripts/FaceDetect_.py

步骤2:修改Cloud Function代码中的路径配置

把硬编码的本地路径替换为相对于当前Cloud Function代码的路径,用Node.js的__dirname变量(表示当前脚本所在目录)来构建正确的路径:

如果脚本和index.js同目录:

var options={
  mode:'text',
  pythonPath: 'python3',  // 云端环境默认提供python3,指定这个避免版本混淆
  pythonOptions: ['-u'],
  scriptPath: __dirname,  // 用当前脚本所在目录作为脚本路径
  args:[ tempLocalFile]
};

如果脚本放在scripts子目录:

const path = require('path');

// ...其他代码

var options={
  mode:'text',
  pythonPath: 'python3',
  pythonOptions: ['-u'],
  scriptPath: path.join(__dirname, 'scripts'),  // 拼接子目录路径
  args:[ tempLocalFile]
};

步骤3:确保部署时包含Python脚本

部署Cloud Function的时候,要确保FaceDetect_.py(或者scripts文件夹)在部署的文件列表里,不要只部署index.js和package.json。如果用gcloud命令部署,直接在函数目录下运行部署命令即可:

gcloud functions deploy Test --runtime nodejs16 --trigger-resource your-bucket-name --trigger-event google.storage.object.finalize

修改后的完整代码示例

const functions = require('firebase-functions'); 
const mkdir=require('mkdirp-promise'); 
const gcs=require('@google-cloud/storage')(); 
const spawn=require('child-process-promise').spawn; 
const path=require('path'); 
const os=require('os'); 
const fs=require('fs'); 
var pythonShell=require('python-shell'); 

exports.Test = functions.storage.object().onFinalize((object) => { 
  const filePath = object.name; 
  const fileName = path.basename(filePath, path.extname(filePath)); 
  const fileDir = path.dirname(filePath); 
  const fileBucket=object.bucket; 
  const tempLocalFile = path.join(os.tmpdir(), filePath); 
  const tempLocalDir = path.dirname(tempLocalFile); 
  const bucket=gcs.bucket(fileBucket); 

  if (object.resourceState === 'not_exists') { 
    console.log('This is a deletion event.'); 
    return; 
  } 

  return mkdir(tempLocalDir)
  .then(()=>{ 
    return bucket.file(filePath).download({destination : tempLocalFile}); 
  })
  .then(()=>{ 
    console.log('Download is complete %j',tempLocalFile); 
    var options={ 
      mode:'text',
      pythonPath: 'python3',
      pythonOptions: ['-u'],
      scriptPath: __dirname,  // 关键修改:使用当前脚本所在目录
      args:[ tempLocalFile] 
    }; 
    pythonShell.run('FaceDetect_.py',options,function(err,results){ 
      if(err) throw err; 
      console.log('result: %j',results); 
    }); 
  }); 
}); 

额外注意事项

  • 云端环境的Python版本:默认提供Python3,所以设置pythonPath: 'python3'更稳妥,避免用python可能指向Python2的问题。
  • 依赖检查:如果你的FaceDetect_.py用到了第三方库(比如OpenCV),需要确保Cloud Function环境里已经安装了这些依赖。可以通过在函数目录下添加requirements.txt,部署时会自动安装依赖;如果依赖比较特殊,可能需要使用自定义运行时。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:53:35