By using AWS re:Post, you agree to the Terms of Use

Using python-magic in a lambda


Hi, I'm trying to run a python lambda function. I'm using the zip deployment. I want to be able to run my code in python 3.9.

The function runs fine using python 3.7 runtime, however when I set the runtime to python 3.9 it fails it gives the error:

"Unable to import module 'lambda_function': failed to find libmagic. Check your installation".

I followed the instructions given in this stack overflow post:

I added a copy of to a directory called lib in the code. However it still gave the same error message. Any help would be appreciated.

  • Better convert you lambda code in SAM. and test it on local with both runtime.
    you might need to add dependency in requriements.txt

asked 22 days ago48 views
2 Answers

Given that the function runs correctly under Python 3.7 but doesn't when you change the runtime I suspect that there are some components in the ZIP which are not compatible with Python 3.9. Rebuild the entire ZIP from scratch using Python 3.9 on your source machine and then upload the new ZIP to Lambda.

answered 22 days ago
  • I did build the package using python 3.9.

  • What platform did you build it on?

  • I built it on an ec2 instance running ubuntu (x86)


Convert you lambda in SAM template. and test it on you system. SAM local invoke <Lambda_NAM> --events <EVENT FILE>

profile picture
answered 21 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions