问题
I have been trying to import python module "Pulp" to the Amazon AWS Lambda but getting an error. Pulp is an optimization module which can be installed using pip ("pip install pulp") but as in AWS Lambda I'm not sure how to install it so I zipped everything along with Lambda Function from my local machine and uploaded it to AWS Lambda.
The error which I received:-
"Attempted relative import in non-package: ValueError Traceback (most recent call last): File "/var/task/lambda_function.py", line 5, in lambda_handler import pulp File "/var/task/pulp.py", line 101, in from .constants import * ValueError: Attempted relative import in non-package"
Here is the link for .zip file https://drive.google.com/open?id=0B7SjHToKYgr3cXlHenpoOFljMDg
Thanks in advance.
回答1:
Follow the instructions on http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html
On how to deploy to AWS lambda using virtualenv.
回答2:
The error is a standard Python error saying that it will only import things from the standard locations. Since that includes the current directory you should be good but your code includes
from . import pulp
when you have pulp.py in your directory. If pulp.py was all that was needed you would be fine. But pulp.py wants to import .constants
. That is a relative reference which would be fine since it is supposed to be in a module. In your case it is not fine. If you want to keep going on this path you will have to go through and remove these relative imports.
Also your .zip file includes .pyc files. Let these be generated on the target machine. Just send your .py's that they came from.
来源:https://stackoverflow.com/questions/45431487/importing-python-module-pulp-on-amazon-aws-lambda