0

I have written a lambda function for AWS which will use pandas for handling dataframe. When I tested this lambda function - I faced error - No module name pandas. I further kept pandas and other dependencies libraries in library folder of my repository.

Now I am facing other issue which I am unable to solve.

Current error:

module 'pandas' has no attribute 'read_csv': AttributeError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 127, in lambda_handler
    initial_df = pd.read_csv(obj['Body']) # 'Body' is a key word
AttributeError: module 'pandas' has no attribute 'read_csv'

I checked the solutions available on this site - like - module 'pandas' has no attribute 'read_csv

I don't have pandas.py and csv.py in my pandas folder but rather have test_to_csv.py, csvs.py and test_pandas.py, which is required as per the discussion in link provided above.

I am unable to figure out a way here.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
D kashyap
  • 25
  • 7
  • What do you mean by "I further kept pandas and other dependencies libraries in library folder of my repository"? You can't just copy the files. You have to use `pip install` to INSTALL pandas in order to use it. pandas is a huge library that installs many other large libraries. And you can't have a folder called `pandas`, because Python will try to import that instead. – Tim Roberts Apr 28 '22 at 06:29
  • Hi, to use pandas with AWS lambda I need to bundle the Pandas library from the Python site-packages off your system. Place it into a lambda_libs\pandas folder (in my repo). Then in my lambda_function.py folder, I place a lambda_libs.txt folder,Added the line pandas to it. Then my pandas library bundled (zipped) into the lambda folder on deployment. This is the procedure I used and it worked too. But now I am getting this error of read_csv. – D kashyap Apr 28 '22 at 06:43
  • https://towardsdatascience.com/python-packages-in-aws-lambda-made-easy-8fbc78520e30 This helped me. Adding layers has solved the errors and also reduced my file size. Now I can edit my code inline too, which was not possible earlier. – D kashyap Apr 28 '22 at 07:40

1 Answers1

0

Pandas is indeed not available by default on AWS lambda. If you want to use Pandas with AWS lamdba, the easiest way is to use the AWS Data Wrangler layer. When you add a new layer, select AWS layers , then in the dropdown menu you can select the AWSDataWrangler-Python39 one. Once you have added the layer, you will be able to use pandas as usual.

robinood
  • 1,138
  • 8
  • 16