title: Unable to import lxml etree on aws lambda
body: <pre><code>{
    "errorMessage": "Unable to import module 'lambda_function': 
          cannot import name   'etree' from 'lxml' (/var/task/lxml/__init__.py)",
    "errorType": "Runtime.ImportModuleError"
  }
  </code></pre>
  
  <p>Also tried <a href="https://gist.github.com/allen-munsch/ad8faf9c04b72aa8d0808fa8953bc639" rel="noreferrer">https://gist.github.com/allen-munsch/ad8faf9c04b72aa8d0808fa8953bc639</a>:</p>
  
  <pre><code>{
    "errorMessage": "Unable to import module 'lambda_function': 
       cannot import name 'etree' from 'lxml' 
       (/var/task/lxml-4.3.4-py3.6-linux-x86_64.egg/lxml/__init__.py)",
    "errorType": "Runtime.ImportModuleError"
  }
  </code></pre>
  
  <p>I am running on <code>Ubuntu 18.04</code> on my local machine, and have also tried using the "Amazon Linux" image on an ec2 instance to build the bundle.</p>
  
  <p>I have also tried, while within the activated venv:</p>
  
  <pre><code>STATIC_DEPS=true pip3 install lxml --target ./package --upgrade --no-cache-dir
  </code></pre>
  
  <p>I have also tried copying the shared object files based on pulling which files were opened when running the script with <code>strace</code>:</p>
  
  <pre><code>#! /bin/bash
  
  export Z=$(pwd)/ok-daily-lambda.zip
  rm $Z
  zip $Z lambda_function.py
  zip $Z __init__.py
  
  for dir in $(find venv_here/lib/python3.6/site-packages)
  do
      if [ -d $dir ] ; then
          pushd $dir
          echo zip -r9 $Z $(pwd)
          zip -r9 $Z $(pwd)
          popd
      fi
  done
  
  export LIBD=$(pwd)/lib
  mkdir -p $LIBD
  
  cp "/home/jmunsch/.local/lib/python3.6/site-packages/.libs_cffi_backend/libffi-d78936b1.so.6.0.4" $LIBD
  cp "/lib/x86_64-linux-gnu/libbz2.so.1.0" $LIBD
  cp "/lib/x86_64-linux-gnu/libc.so.6" $LIBD
  cp "/lib/x86_64-linux-gnu/libdl.so.2" $LIBD
  cp "/lib/x86_64-linux-gnu/libexpat.so.1" $LIBD
  cp "/lib/x86_64-linux-gnu/libgcc_s.so.1" $LIBD
  cp "/lib/x86_64-linux-gnu/liblzma.so.5" $LIBD
  cp "/lib/x86_64-linux-gnu/libm.so.6" $LIBD
  cp "/lib/x86_64-linux-gnu/libnss_dns.so.2" $LIBD
  cp "/lib/x86_64-linux-gnu/libnss_files.so.2" $LIBD
  cp "/lib/x86_64-linux-gnu/libnss_mdns4_minimal.so.2" $LIBD
  cp "/lib/x86_64-linux-gnu/libpthread.so.0" $LIBD
  cp "/lib/x86_64-linux-gnu/libresolv.so.2" $LIBD
  cp "/lib/x86_64-linux-gnu/librt.so.1" $LIBD
  cp "/lib/x86_64-linux-gnu/libtinfo.so.5" $LIBD
  cp "/lib/x86_64-linux-gnu/libudev.so.1" $LIBD
  cp "/lib/x86_64-linux-gnu/libutil.so.1" $LIBD
  cp "/lib/x86_64-linux-gnu/libuuid.so.1" $LIBD
  cp "/lib/x86_64-linux-gnu/libz.so.1" $LIBD
  cp "/usr/lib/x86_64-linux-gnu/libapt-pkg.so.5.0" $LIBD
  cp "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1" $LIBD
  cp "/usr/lib/x86_64-linux-gnu/libffi.so.6" $LIBD
  cp "/usr/lib/x86_64-linux-gnu/liblz4.so.1" $LIBD
  cp "/usr/lib/x86_64-linux-gnu/libmpdec.so.2" $LIBD
  cp "/usr/lib/x86_64-linux-gnu/libssl.so.1.1" $LIBD
  cp "/usr/lib/x86_64-linux-gnu/libstdc++.so.6" $LIBD
  cp "/usr/lib/x86_64-linux-gnu/libzstd.so.1" $LIBD
  
  zip -r $Z $LIBD
  
  AWS_ACCESS_KEY_ID="xxx" AWS_SECRET_ACCESS_KEY="xxx" AWS_DEFAULT_REGION="us-east-1" aws lambda update-function-code --function-name ok-today --zip-file fileb://ok-daily-lambda.zip
  </code></pre>
  
  <p>Here is the directory structure of the most recent zip file: </p>
  
  <ul>
  <li><a href="https://gist.github.com/allen-munsch/5ddc27873e64db6ff106ab828febf434" rel="noreferrer">https://gist.github.com/allen-munsch/5ddc27873e64db6ff106ab828febf434</a></li>
  </ul>
  
  <p>Related:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/36397171/aws-lambda-not-importing-lxml">AWS Lambda not importing LXML</a></li>
  <li><a href="https://stackoverflow.com/questions/12071547/lxml-cannot-import-etree">LXML: Cannot import etree</a></li>
  <li><a href="https://lxml.de/installation.html" rel="noreferrer">https://lxml.de/installation.html</a></li>
  <li><a href="https://lxml.de/build.html" rel="noreferrer">https://lxml.de/build.html</a></li>
  <li><a href="https://stackoverflow.com/questions/21779155/lxml-cannot-import-element-etree">lxml cannot import element etree</a></li>
  <li><a href="https://stackoverflow.com/questions/55287578/shared-libraries-in-java-aws-lambda-project">Shared Libraries in Java AWS Lambda Project</a></li>
  <li><a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html" rel="noreferrer">https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html</a></li>
  </ul>