AWS Lambda not importing LXML

前端 未结 8 1409
傲寒
傲寒 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:18

    I was able to get this working by following the readme on this page:

    1. With docker installed, run this command (replacing python3.8 with the version of python you are using for your lambda function, and lxml with the version of lxml you want to use)
      $ docker run -v $(pwd):/outputs -it lambci/lambda:build-python3.8 \
            pip install lxml -t /outputs/
      
    2. This will create a folder called lxml in your working directory, and possibly some other folders which you can ignore. Move the lxml folder to the same directory as the .py file you are using as your lambda handler.
    3. Zip up the .py file with the lxml folder, as well as any packages if you are using a virtualenv. I had a virtualenv and lxml already existed in my site-packages folder, so I had to delete it first. Here are the commands I ran (note that my virtualenv v-env folder was in the same directory as my .py file):
      FUNCTION_NAME="name_of_your_python_file"
      cd v-env/lib/python3.8/site-packages &&
      rm -rf lxml &&
      rm -rf lxml-4.5.1.dist-info &&
      zip -r9 ${OLDPWD}/${FUNCTION_NAME}.zip . &&
      cd ${OLDPWD} &&
      zip -g ${FUNCTION_NAME}.zip ${FUNCTION_NAME}.py && 
      zip -r9 ${FUNCTION_NAME}.zip lxml
      
    4. If you don't have a virtualenv or any other dependencies, you can just run
      FUNCTION_NAME="name_of_your_python_file"
      zip -g ${FUNCTION_NAME}.zip ${FUNCTION_NAME}.py && 
      zip -r9 ${FUNCTION_NAME}.zip lxml
      
    5. Upload ${FUNCTION_NAME}.zip to your lambda function and use as normal.

    More on creating a .zip file for lambda with a virtualenv here

提交回复
热议问题