Subprocess.check_output error with error 127

I am trying to call an external program from my python application, but it does not show output and does not work with error 127. Running the command from the command line works fine. (and I'm in the right working directory)

def buildContris (self, startUrl, reportArray): urls = [] for row in reportArray: try: url = subprocess.check_output(["casperjs", "casper.js", startUrl, row[0]], shell=True) print (url) urls.append(url) break except subprocess.CalledProcessError as e: print ("Error: " + str(e.returncode) + " Output:" + e.output.decode()) return urls 

Each loop throws the following error: (I also checked e.cmd. It is correct but long, so I omitted it in this example)

 Error: 127 Output: 

DECISION:

The following code works

 app = subprocess.Popen(["./casperjs/bin/casperjs", "casper.js", startUrl, row[0]], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env = {"PATH" : "/usr/local/bin/:/usr/bin"}, universal_newlines=True) app.wait() out, errs = app.communicate() 
+5
source share
4 answers

Try adding the full path to casperjs in your subprocess.check_output () call.

Edit: Answering your second question. Sorry for the formatting since I'm on an iPad. I think you should try Popen instead of check_output so that you can specify environment variables:

 p = subprocess.Popen(["/path/to/casperjs", "casper.js", startUrl, row[0]], env={"PATH": "/path/to/phantomjs"}) url, err = p.communicate() 
+3
source

shell=True changes the interpretation of the first argument ( args ) in the call to check_output() , from the documents :

On Unix with shell = True, ... If args is a sequence, the first element indicates the command line, and any additional elements will be considered as additional arguments for the shell itself. That is, Popen makes the equivalent:

 Popen(['/bin/sh', '-c', args[0], args[1], ...]) 

An exit status of 127 may mean that the shell did not detect that the casperjs or casperjs itself exited with this code.

To fix the code: release shell=True and specify the full path to the casperjs program, for example:

 url = check_output(["./casperjs", "casper.js", startUrl, row[0]]) 
0
source

Try explicitly adding the path this way. If the file to call is in the same path (change __file__ if not):

 cwd=os.path.dirname(os.path.realpath(__file__)) a = subprocess.check_output(["./casper.js", startUrl, row[0]],cwd=cwd,shell=True) 
0
source

If you experience such stupidity in macOS: do not use aliases . Lost half a day with this. So change:

 subprocess.check_output( "scribus-ng -g -ns -py {0} {1}".format(script_path, id), stderr=subprocess.STDOUT, shell=True) 

in

 subprocess.check_output( "/Applications/Scribus.app/Contents/MacOS/Scribus -g -ns -py {0} {1}".format(script_path, id), stderr=subprocess.STDOUT, shell=True) 
0
source

Source: https://habr.com/ru/post/1500847/


All Articles