简体   繁体   English

在 Cloud Function 的文件中运行 Python 脚本

[英]Run Python script in a file from Cloud Function

I want to run python script in server (Google Compute Engine).我想在服务器(谷歌计算引擎)中运行 python 脚本。 So I made a Cloud Function code using python-shell.所以我使用 python-shell 制作了一个 Cloud Function 代码。 However this error appeared.但是出现了这个错误。

Error: python: can't open file '/home/dmproject0608/test2/FaceDetect_.py': [Errno 2] No such file or directory错误:python:无法打开文件“/home/dmproject0608/test2/FaceDetect_.py”:[Errno 2] 没有这样的文件或目录

And this is my Cloud Function:这是我的云 Function:

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:'',
            pythonOptions: ['-u'],
            scriptPath:'/home/dmproject0608/test2',
            args:[ tempLocalFile]
        };
        pythonShell.run('FaceDetect_.py',options,function(err,results){
            if(err) throw err;
            console.log('result: %j',results);
        });
    });
});

May be a bit late for this response, try absolute paths when using Python-shell.此响应可能有点晚,请在使用 Python-shell 时尝试绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud Function - Python 从 Webhook 获取数据的脚本 - Google Cloud Function - Python script to get data from Webhook 云从云存储运行和挂载文件 - cloud run and mount a file from cloud storage Firebase 云函数运行 Python 脚本 - Firebase Cloud Functions to run Python script 如何使用 python 脚本提取多部分的 zip 文件以在 Google Cloud Function 中运行 - How to extract zip files with multi parts using python script to run in Google Cloud Function 谷歌云数据流(Python):function 读取和写入 a.csv 文件? - Google Cloud Dataflow (Python): function to read from and write to a .csv file? 如何使用 Google Cloud Run 运行“hello world”python 脚本 - How to run a "hello world" python script with Google Cloud Run 从云端在 GKE 中运行作业 function - Run Job in GKE from a cloud function 保护从 gcp 云函数到 Cloud run Api 的 api 调用 - Protect api call from gcp cloud function to Cloud run Api 您可以从 Cloud Function 内部调用 Cloud Run 应用程序吗? - Can you call a Cloud Run app from inside a Cloud Function? 如何从 Java 脚本安全地调用 Google Cloud Run 服务? - How to securely call a Google Cloud Run service from Java Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM