2

I have downloaded a script from git into /tmp directory and I need to run the script in lambda. My handler looks like this:

def handler(event, context):
  process = subprocess.run("/tmp/my_script.py", env = os.environ, stdout=None, stderr=subprocess.STDOUT)

However, my_script.py has some external dependencies, such as gevent.

# my_script.py
import gevent
...

When running in lambda, I got the following error:

ModuleNotFoundError: No module named 'gevent'

I have packaged the gevent module in the zipped file uploaded to lambda, which I believe they are under /var/task. How can I let my_script.py in /tmp directory know where to look for the dependencies?

2 Answers 2

2

I solved this problem by adding "PYTHONPATH" = "/var/task" in env.

os.environ["PYTHONPATH"] = "/var/task"
process = subprocess.run("/tmp/my_script.py", env = os.environ, stdout=None, stderr=subprocess.STDOUT)
Sign up to request clarification or add additional context in comments.

Comments

0

You could try inserting /var/task into the path, like so:

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../var/task')))

import gevent

Assuming the script above is in /tmp and gevent module is in /var/task.

2 Comments

Thank you for the answer! But the problem is that the script is cloned from git and I cannot edit it. Or should I even edit it (That seems hacky)? I was wondering if there is a way to specify the path when calling subprocess maybe?
Problem solved! Adding "PYTHONPATH" = "/var/task" in env solved the problem.

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.