简体   繁体   中英

Run python script from Node.js

I'm trying to run a simple python script from Node.js, i have installed python-shell package and this is my code:

var PythonShell = require('python-shell');

var options = {
  mode: 'text',
  pythonPath: '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/',
  pythonOptions: ['-u'],
  scriptPath: '.',
  args: ['value1', 'value2', 'value3']
};

PythonShell.run('my_script.py', options, function (err, results) {
  if (err) throw err;
  // results is an array consisting of messages collected during execution
  console.log('results: %j', results);
});

Python:

import sys

arg1 = sys.argv[1] #value1
arg2 = sys.argv[2] #value2
arg3 = sys.argv[3] #value3

print arg1, arg2, arg3

Executing node test.js i have this error:

Error: spawn EACCES
    at exports._errnoException (util.js:907:11)
    at ChildProcess.spawn (internal/child_process.js:298:11)
    at exports.spawn (child_process.js:362:9)
    at new PythonShell (/Users/Antonio/node_modules/python-shell/index.js:59:25)
    at Function.PythonShell.run (/Users/Antonio/node_modules/python-shell/index.js:159:19)
    at Object.<anonymous> (/Users/Antonio/Desktop/script/prova.js:11:13)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)

I have set permissions to my_script.py using sudo chmod -R a+rwx my_script.py . How can i solve this error?

To resolve the error you need to fix the string containing the Path or location of Python.

Not PATH as in a setup PATH to access modules, API's, Library and whatever other files may be needed. Rather the regular location on the drive it is located. 'directory map' or location( shown when searching folders example, /usr/bin/python also can be /usr/bin/python2.7) this depends on where the actual .exe python file is. So give the directions (or for lack of a better word Path but not PATH that you set up like the system path or any custom paths are wrong). The simple location of the executable should be in the string, so it should not end with a / either.

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