简体   繁体   中英

Python in AWS Lambda giving unexpected unindent error

When I try to test my python function in Lambda, I'm getting this error below:

"errorMessage": "Syntax error in module 'lambda_function'"

And this error in the CloudWatch logs:

Syntax error in module 'lambda_function': unexpected unindent (lambda_function.py, line 28)

Here is my python code:

from __future__ import print_function
import urllib2 
from multiprocessing.dummy import Pool as ThreadPool 

import hashlib
import datetime
import json

print('Loading function')

def my_urlopen(url):
    try:
        return urllib2.urlopen(url)
    except Exception as e:
        try:
            return urllib2.urlopen(url)
        except Exception as e2:
            urllib2.urlopen("https://example.com/cron/error.php?url="+url+"&code="+str(e2.code));#+"&reason="+e2.reason);
            return None
        return None

def customer_list(cron_cipher, minute):
    try:
        return urllib2.urlopen("https://d-example.com:444/TESTcron.php?k="+cron_cipher+"&m="+minute+"&f=rules")
    except Exception as e:
        try:
            return urllib2.urlopen("https://e-example.com:444/TESTcron.php?k="+cron_cipher+"&m="+minute+"&f=rules")
        except Exception as e2:
            urllib2.urlopen("https://example.com/cron/error.php?url="+url+"&code="+str(e2.code))
            print("Lookup error: https://example.com/cron/error.php?url="+url+"&code="+str(e2.code));
            return None
        return None

def lambda_handler(event, context):
    # code continues below....

I'm extremely new to python, but the code was working using the my_urlopen function as it shows here, but the addition of the customer_list function seems to be causing a problem, though I cannot see the syntax issue.

Line 28 is the except Exception as e2: line in the customer_list function.

It appears to be indented the correct amount, and I think the semicolons aren't needed (though I have tried both with and without). What am I missing?

The code you pasted into the question has a mixture of spaces and tabs used for indentation. In Python that is a no-no. You must either use all spaces or all tabs for indentation. The PEP8 style guide says spaces are the preferred indentation method .

Be sure to use a text editor which has a "whitespace-mode" which allows you to see the difference between spaces and tabs. For example, emacs's Mx whitespace-mode indicates tabs highlighted in yellow and the spaces as centered dots.

在此处输入图片说明

If you are using unix, another way to detect tabs vs spaces is to run cat -A <filename> :

% cat -A lambda_function.py
def customer_list(cron_cipher, minute):$
^Itry:$
^I^Ireturn urllib2.urlopen("https://d-example.com:444/TESTcron.php?k="+cron_cipher+"&m="+minute+"&f=rules")$
^Iexcept Exception as e:$
^I^Itry:$
^I^I^Ireturn urllib2.urlopen("https://e-example.com:444/TESTcron.php?k="+cron_cipher+"&m="+minute+"&f=rules")$
        except Exception as e2:$
            urllib2.urlopen("https://example.com/cron/error.php?url="+url+"&code="+str(e2.code))$
^I^I    print("Lookup error: https://example.com/cron/error.php?url="+url+"&code="+str(e2.code));$
            return None$
        return None$
$
def lambda_handler(event, context):$
^I    # code continues below....$

Here, the tabs are depicted by ^I .


Replace the tabs with 4 spaces to fix the syntax error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM