1

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?

1
  • Try to run locally first Commented Jul 29, 2017 at 1:38

1 Answer 1

3

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 M-x whitespace-mode indicates tabs highlighted in yellow and the spaces as centered dots.

enter image description here

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.