AWS Lambda not importing LXML

前端 未结 8 1426
傲寒
傲寒 2021-01-02 04:57

I am trying to use the LXML module within AWS Lambda and having no luck. I downloaded LXML using the following command:

pip install lxml -t folder

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-02 05:22

    Expanding a bit on Mask's answer. In the case of installing lxml in particular, the libxslt and libxml2 libraries are already installed on the AMI that executes the AWS lambda. Therefore it is no need to start a subprocess with a different LD_LIBRARY_PATH as in that answer, it is however necessary to run pip install lxml on an AMI image (it might be possible to cross-compile as well but I don't know how).

    Launch an ec2 machine with Amazon Linux ami
    Run the following script to accumulate dependencies:
    set -e -o pipefail
    sudo yum -y upgrade
    sudo yum -y install gcc python-devel libxml2-devel libxslt-devel
    
    virtualenv ~/env && cd ~/env && source bin/activate
    pip install lxml
    for dir in lib64/python2.7/site-packages \
        lib/python2.7/site-packages
    do
        if [ -d $dir ] ; then
            pushd $dir; zip -r ~/deps.zip .; popd
        fi
    done 
    

    Note that the last steps from Marks answer is left out. You can use lxml straight from the python file that contains the handler method.

提交回复
热议问题