简体   繁体   中英

Run Python script in a file from Cloud Function

I want to run python script in server (Google Compute Engine). So I made a Cloud Function code using python-shell. However this error appeared.

Error: python: can't open file '/home/dmproject0608/test2/FaceDetect_.py': [Errno 2] No such file or directory

And this is my Cloud 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM