2017-01-06 23 views
0

我想在AWS上的Lambda函数中使用Python拼写检查库Pyenchant。 Pyenchant是C libenchant库的封装,它依赖于Aspell等提供者的单词词典。在AWS Lambda上使用Pyenchant,可以加载包而不是提供者字典,已经从EC2上的源代码编译并提取.so文件

在我运行在Lambda上的Python代码中,我能够成功导入已编译它的附魔库和C库(libenchant.so)到AWS Linux EC2实例并将输出复制到我的Lambda部署包。

但是,pyenchant库在Lambda上运行时无法加载任何需要工作的单词词典。

yum install aspell-en enchant-aspell 

我再复制以下附加.so文件到我的部署包的/ lib文件夹:

  • libaspell.so
  • libenchant_aspell.so
  • 我然后使用安装在安博泰EC2实例
  • libenchant_ispell.so
  • libenchant_myspell.so
  • libenchant.so

我敢肯定libenchant_aspell.so是实际的字典,但它没有拿起它,我无法找出下一步去哪里。

下面是我lambda_handler Python代码:

from __future__ import print_function 
import os 
import sys 
import re 
import enchant 

enchant.set_param("enchant.aspell.dictionary.path","/var/task/lib") 

def lambda_handler(event, context): 

    print("# List available enchant dictionary languages") 
    print(enchant.list_languages()) 
    b = enchant.Broker() 
    print("# List available enchant brokers") 
    print(b.describe()) 
    d = enchant.Dict("en_GB") 
    # print(d.provider.name) 
    # print(d.provider.file) 
    return "Done" 

这里是从调用lambda函数的输出:

START RequestId: 7539245b-d3d6-11e6-b7e6-edc1dc8cbdd4 Version: $LATEST 
# List available enchant dictionary languages 
[] 
# List available enchant brokers 
[] 
Dictionary for language 'en_GB' could not be found: DictNotFoundError 
Traceback (most recent call last): 
    File "/var/task/package_test.py", line 16, in lambda_handler 
    d = enchant.Dict("en_GB") 
    File "/var/task/enchant/__init__.py", line 558, in __init__ 
    _EnchantObject.__init__(self) 
    File "/var/task/enchant/__init__.py", line 168, in __init__ 
    self._init_this() 
    File "/var/task/enchant/__init__.py", line 565, in _init_this 
    this = self._broker._request_dict_data(self.tag) 
    File "/var/task/enchant/__init__.py", line 310, in _request_dict_data 
    self._raise_error(eStr % (tag,),DictNotFoundError) 
    File "/var/task/enchant/__init__.py", line 258, in _raise_error 
    raise eclass(default) 
DictNotFoundError: Dictionary for language 'en_GB' could not be found 

END RequestId: 7539245b-d3d6-11e6-b7e6-edc1dc8cbdd4 
REPORT RequestId: 7539245b-d3d6-11e6-b7e6-edc1dc8cbdd4 Duration: 1.03 ms Billed Duration: 100 ms  Memory Size: 256 MB Max Memory Used: 16 MB 

正如你可以看到import enchant工作正常,但无法找到任何的字典文件。

我真的被困在这,花了6个小时的最佳部分试图找出如何得到这个工作。在此先感谢您的帮助。

回答

0

那么,对于任何遇到这个问题的人(这可能是没有人......)事实证明,这是不可能在Lambda上使用这个包。与没有合适的基础设施来加载共享对象资源几个级别深有关。最后,我在EC2上使用了一个Web服务器,它工作正常。

相关问题