2017-01-23 26 views
0

Ich habe die Datei app.py im Stammverzeichnis meiner app.zip-Datei. Und die Funktion Handler auch richtig definiert (lambda_handler), nach dem Handler Config .: app.lambda_handlerPython: Modul-App kann nicht in AWS Lambda importiert werden

Dennoch erhalte ich die Fehlermeldung: Unable to import module 'app': No module named app

Wo habe ich falsch gemacht?

Mein Skript:

from __future__ import print_function 

import json 
import urllib 
import boto3 
from collections import Counter 
from nltk.tokenize import sent_tokenize, word_tokenize 
from nltk.corpus import stopwords 
from nltk.tokenize import RegexpTokenizer 
from nltk.stem.porter import * 
from nltk.corpus import stopwords 
from nltk.tokenize import RegexpTokenizer 
tokenizer = RegexpTokenizer(r'\w+') 
stemmer=PorterStemmer() 
import sys 
reload(sys) 
sys.setdefaultencoding('utf8') 


print('Loading function') 

s3 = boto3.client('s3') 

number_of_sentences=0 
number_of_words=0 
word_list=[] 
stop_words=set(stopwords.words('english')) 
stop_word_list=[ v for v in stop_words] 
modal_verbs=['can', 'could', 'may', 'might', 'must', 'shall', 'should', 'will' ,'would','ought'] 
auxilary_verbs=['be','do','have'] 
stop_word_list=stop_word_list+modal_verbs+auxilary_verbs 
print("Starting Trigram generation") 
#Empty Trigram list 
tri_gram_list=[] 

def lambda_handler(event, context): 
    #print("Received event: " + json.dumps(event, indent=2)) 

    # Get the object from the event and show its content type 
    ''' 
    ''' 
    bucket = event['Records'][0]['s3']['bucket']['name'] 
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8')) 
    try: 
     response = s3.get_object(Bucket=bucket, Key=key) 
     print("CONTENT TYPE: " + response['ContentType']) 
     text = response['Body'].read() 
     print(type(text)) 
     for line in text.readlines(): 
      for line in open("input.txt","r").readlines(): 
       line=unicode(line, errors='ignore') 
       if len(line)>1: 
        sentences=sent_tokenize(line) 
        number_of_sentences+=len(sentences) 
        for sentence in sentences: 
         sentence=sentence.strip().lower() 
         #sentence = sentence.replace('+', ' ').replace('.', ' ').replace(',', ' ').replace(':', ' ').replace('(', ' ').replace(')', ' ').replace(''`'', ' ').strip().lower() 
         words_from_sentence=tokenizer.tokenize(line) 
         words = [word for word in words_from_sentence if word not in stop_word_list] 
         number_of_words+=len(words) 
         stemmed_words = [stemmer.stem(word) for word in words] 
         word_list.extend(stemmed_words) 
         #generate Trigrams 
         tri_gram_list_t= [ " ".join([words[index],words[index+1],words[index+2]]) for index,value in enumerate(words) if index<len(words)-2] 
         #print tri_gram_list 
         tri_gram_list.extend(tri_gram_list_t) 

     print number_of_words 
     print number_of_sentences 
     print("Conting frequency now...") 
     count=Counter() 
     for element in tri_gram_list: 
      #print element, type(tri_gram_list) 
      count[element]=count[element]+1 
     print count.most_common(25) 
     print "most common 25 words ARE:" 
     for element in word_list: 
      #print element, type(tri_gram_list) 
      count[element]=count[element]+1 
     print count.most_common(25) 




     # body = obj.get()['Body'].read() 

    except Exception as e: 
     print(e) 
     print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket)) 
     raise e 

Wo habe ich falsch gemacht?

+0

Sie eine haben Sie \ _ \ _ init \ _ \ _ py in Ihrem Verzeichnis.? – denvaar

+0

Woher haben Sie das Skript ausgeführt? Das Modul, das Sie importieren möchten, muss sich entweder im Systempfad oder im aktuellen Pfad befinden. – Flickerlight

+0

@denvaar Nein Muss ich es haben? – Dawny33

Antwort

0

Versuchen Sie, die Protokollausgabe zu überprüfen. Es gibt Ihnen mehr Informationen als der Fehler, den Sie oben gesehen haben.

Schließlich erinnern, dass Sie die Python 2 Syntax benötigen, ersetzen Anrufe wie:

print number_of_words von print(number_of_words)

Verwandte Themen