title: AWS lambda: Importing external python library using subprocess makes program too slow
body: <p>My use case is:</p>
  
  <p>I have to execute some python scripts on AWS Lambda.
  I am using <strong>subprocess.check_output</strong> to execute those scripts and those works fine.
  Problem arises when the scripts has some external dependencies (not on Lambda by default) e.g numpy.</p>
  
  <p>So, I created my deployment package with the external libraries which now reside in <strong>/opt/python/lib/python2.7/site-packages</strong> and I am able to import them in my handler function.</p>
  
  <p>But when I execute the scripts, my scripts were not able to find those libraries. So, I looked around and found out that subprocess doesn't preserve PYTHONPATH. </p>
  
  <p>So, now I am passing env parameter to the subprocess.check_output, through which those scripts are able to find the libraries. </p>
  
  <p>But the issue is, this makes the execution extremely slow. (e.g A program runs in 300 ms which doesn't have any external dependency import, but with external dependency import it takes 4883.81 ms).</p>
  
  <p>My handler function looks like this:</p>
  
  <pre><code>import subprocess
  
  def main(event, context):
      # Omitting creation of script.py in /tmp directory. 
      print(subprocess.Popen(["/usr/bin/python", "/tmp/script.py"], env={'PYTHONPATH': '/opt/python/lib/python2.7/site-packages'}).communicate())
  
  </code></pre>
  
  <p>Also tried:</p>
  
  <ol>
  <li>Changing working directory  by <code>**cwd**</code> parameter.</li>
  <li>Installing external dependencies in same folder as of handler function.</li>
  <li>Tried with <strong>subprocess.check_output</strong> instead of <strong>Popen</strong></li>
  </ol>
  
  <p>Any lead is appreciated. Thanks in Advance.
  Also please let me know, if you need any other information.</p>
  
  <p>Edit: Above examples uses <strong>lambda layer</strong> for the python packages, But I have tried without lambda layer (where Python packages are in the same folder as of handler.py) </p>