python-dependency-injector
python-dependency-injector copied to clipboard
Not able to import 'providers' from 'dependency_injector' in AWS lambda
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.
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:

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 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 install
ed 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?
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>
@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
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>
Someone was abble to fix this issue by any workarounds?
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,
)