1
votes

I'm having trouble identifying the path to a file in the public directory c:\TEMP\todos\.meteor\local\build\programs\server\public\main.py. Meteor complains the file or directory doesn't exist. Already searched the other postings about the similar issue (e.g., Reading files from a directory inside a meteor app) but didn't help.

Here is the error message.

=> Your application has errors. Waiting for file change.
=> Modified -- restarting.
=> Meteor server restarted
W20151206-04:05:57.893(-5)? (STDERR) Error inside the Async.runSync: ENOENT, no such file or directory 'c:\TEMP\todos\.meteor\local\build\programs\server\public'

Client code

Meteor.call('runPython', function(err, response) {
    if(err){

    } else {
        console.log(response);
    }
})

Server code

Meteor.startup( function (){
    Meteor.methods({
        runPython: function (){
            var PythonShell = Meteor.npmRequire('python-shell');
            var fs = Meteor.npmRequire('fs');

            var runPython = Async.runSync(function (done){
                var files = fs.readdirSync('./public/');

//   PythonShell.run('main.py', function ... was tried first but Meteor complained that "main.py doesn't exist". So below is a different attempt.

                var py = _(files).reject(function(fileName){
                    return fileName.indexOf('.py') <0;
                })

            PythonShell.run(py, function (err) {
// PythonShell.run(path.join(py,"main.py") ... was also tried but resulted in the same error
                if (err) throw err;
                    console.log('script running failed');
                });
        })

        return "Success";
    }
})
})
3
FYI: public should be at the root of your Meteor app, not under server. From the client side the public directory will resolve to /, not /public - Michel Floyd

3 Answers

0
votes

All files inside the public folder should be read using '/':

var files = fs.readdirSync('/');

More here: http://docs.meteor.com/#/full/structuringyourapp

For server-side only (might be your case and probably a better solution) you can put everything under the private/ folder and access them by using the Assets API: http://docs.meteor.com/#/full/assets_getText

0
votes

Clearly I was overthinking it. Specifying a full path to the file was all I needed to do.

PythonShell.run('c:\\project\\public\\main.py', function ...
0
votes

If your application allows moving the Python script to /private instead of /public, you can take advantage of Meteor's Assets:

Assets allows server code in a Meteor application to access static server assets, which are located in the private subdirectory of an application’s tree. Assets are not processed as source files and are copied directly into your application’s bundle.

e.g. If you move your script to /private/scripts/script.py you can generate the absolute path the Meteor way by doing Assets.absoluteFilePath('scripts/script.py').