0

Lets say I have a python project, where using a command in my local terminal I can do something like:

python3 somePythonFile.py --someFlag True --someOtherFlag False -f https://www.somexmlfile/thefile.xml

If I wanted to run this in a python Lambda, would it be possible to run it the same way, without importing as a module.

For example, I have tried this:

import subprocess
from subprocess import Popen

cmd = "python3 somePythonFile.py --someFlag True --someOtherFlag False -f https://www.somexmlfile/thefile.xml"

returned_output = Popen(cmd)

# using decode() function to convert byte string to string
print('Converted result:', returned_output.decode("utf-8"))

However this gives me an error:

[ERROR] FileNotFoundError: [Errno 2] No such file or directory: 'python3 somePythonFile.py --someFlag True --someOtherFlag False -f https://www.somexmlfile/thefile.xml'
Traceback (most recent call last):
  File "/var/task/app.py", line 32, in lambda_handler
    returned_output = Popen(cmd)
  File "/var/lang/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/var/lang/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
user3437721
  • 2,227
  • 4
  • 31
  • 61

1 Answers1

0

The command for Popen() needs to have the command line parameters as separated in a list. https://docs.python.org/3/library/subprocess.html#popen-constructor

Change your line

returned_output = Popen(cmd)

to this

returned_output = Popen(cmd.split())

As far as the lambda goes. I would highly suggest you put this code in a module (might save you lots of money). The AWS Lambda environment will be terminated as soon as the handler function returns. You would need to code your Lambda function to wait for the subprocess to complete.

Thanks to Mark B's answer to this question Independent python subprocess from AWS Lambda function

Back2Basics
  • 7,406
  • 2
  • 32
  • 45
  • thanks, is there a way to convert the returned_output to some sort of string for example? – user3437721 Feb 25 '21 at 11:34
  • Sure. try a different call subprocess.check_output(cmd.split()) https://docs.python.org/3/library/subprocess.html#subprocess.check_output – Back2Basics Feb 25 '21 at 11:50