0

I am trying to get use a python script within my node.js application using the NPM package python-shell. I can run the script from command line using this format on Mac OS.

python3 myScript.py < input.zip > output.wav

However when I try to run the script in my node application I get this error

Error: usage: myScript.py [-h] [-s FACTOR]
myScript.py: error: unrecognized arguments: input output

I get the same error when I remove the < and > from the command in the terminal I have tried to add the < and > to the python-shell options however I get the same error with the < and > included.

Node.js

        const input = `input.zip`;
        const output = `output.wav`;
        const options: any = {
          args: [input,output],
          pythonOptions: ['-u'],
        };


        PythonShell.run('myScript.py', options, function (err, results) {
          if (err) throw err;
          console.log('finished');
          console.log(results);
        });

myScript.py

def main():
    print("started")
    global args
    args = parse_args()
    write_wav(read_chunks(sys.stdin.buffer), sys.stdout.buffer)


if __name__ == '__main__':
    main()

I am really unsure what to do to get this working any advice is greatly appreciated.

1 Answer 1

1

you are providing a var name as parameter, not its value

In nodejs I would code this :

var python_options = {       
  mode: 'text',       
  pythonPath: '/usr/bin/python',
  pythonOptions: ['-u'],
  scriptPath: '/home/pi/njs/proves_js',
  args: ['', '', '']
} ;

python_options.args[0] = "input.zip"

and in python this :

if len(sys.argv) > 2: szInFN  = str( sys.argv[1] )  # get output.zip
if len(sys.argv) > 1: szOutFN = str( sys.argv[0] )  # get input.zip

Good luck

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.