python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Not able to import 'providers' from 'dependency_injector' in AWS lambda

Open imageschool opened this issue 3 years ago • 7 comments

Hi, I am trying to import python-dependency-injector package within lambda function.

All other packages gets imported fine, but with this package I keep getting the below error :\

{"errorMessage": "Unable to import module 'main': cannot import name 'providers' from 'dependency_injector' (/var/task/dependency_injector/__init__.py)", "errorType": "Runtime.ImportModuleError", "requestId": "5c63c01b-5be1-4481-adf8-691167cb54bd", "stackTrace": []}

Would really appreciate if someone can guide me how to resolve this issue.

imageschool avatar Dec 27 '21 09:12 imageschool

Hey @imageschool ,

Such kind of problem may happen if there are no .so files for some reason. Dependency Injector is built on Cython and the actual providers module is not a Python file. See screenshot:

Screen Shot 2022-01-16 at 12 25 33 PM

I would suggest checking how you install Dependency Injector. If you make any manipulations with files, make sure that you don't cut .so files.

rmk135 avatar Jan 16 '22 17:01 rmk135

@rmk135 Hello, I am also running into this same issue. I have ensured that the .so files are present, as well as, ensuring that I pip installed the package on a Docker Image which has the same base OS as the AWS Lambda that I am deploying to. Could you provide any guidance?

rrao24 avatar Jan 25 '22 09:01 rrao24

I was able to solve this problem to support MacOS x86 -> AWS Lambda Graviton2, hoping it might help others.

requirements.txt (I'm using the yaml support):

dependency-injector[yaml]==4.37.0

Then install with the --platform flag (although you may have to change it depending on your arch)

pip install --platform=manylinux2014_aarch64 --only-binary=:all: -r requirements.txt -t <lambda dir>

s0l0ist avatar Jan 28 '22 04:01 s0l0ist

@rmk135 Hello, I'm having the same problem. I install dependency injector into a lambda layer and upload it. The lamdba handler cannot recognize it or import containers . [ERROR] Runtime.ImportModuleError: Unable to import module 'handler': cannot import name 'containers' from 'dependency_injector' (/opt/python/dependency_injector/__init__.py) Traceback (most recent call last):

Anything else seems to get well imported

yaziamoe avatar Oct 28 '22 09:10 yaziamoe

I'm also having the same problem. I tried to build the python package layer using the following but no luck: pip install --platform=manylinux2014_x86_64 --only-binary=:all: -r requirements.txt -t <lambda dir>

yuchen-w avatar Mar 22 '23 18:03 yuchen-w

Someone was abble to fix this issue by any workarounds?

lourencomcviana avatar Mar 28 '23 18:03 lourencomcviana

Thanks, I solved this using docker bundling with a Python 3.9 image and the next command:

pip3.9 install --platform=manylinux2014_x86_64 --only-binary=:all: -r requirements-dependency_injector_layer.txt -t /asset-output/python

Using CDK makes this task very straightforward:

dependency_injector_layer = lambda_.LayerVersion(
    scope=self,
    id="id",
    layer_version_name="name",
    code=lambda_.Code.from_asset(
        str(pathlib.Path(__file__).parent.joinpath("dependencies").resolve()),
        bundling=BundlingOptions(
            image=lambda_.Runtime.PYTHON_3_9.bundling_image,
            command=[
                "bash", "-c",
                "pip3.9 install --platform=manylinux2014_x86_64 --only-binary=:all: -r requirements-dependency_injector_layer.txt -t /asset-output/python"
                " && cp -au . /asset-output/python",
            ]
        )
    ),
    compatible_runtimes=[
        lambda_.Runtime.PYTHON_3_9,
    ],
    removal_policy=RemovalPolicy.RETAIN,
)

lmiguelmh avatar Jan 03 '24 16:01 lmiguelmh