Coding Monkey
Coding Monkey

Reputation: 173

How to run a python script in aws lambda /tmp directory

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?

Upvotes: 2

Views: 1576

Answers (2)

Coding Monkey
Coding Monkey

Reputation: 173

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)

Upvotes: 2

rouhija
rouhija

Reputation: 241

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.

Upvotes: 0

Related Questions