dat*_*ead 5 python aspell pyenchant enchant aws-lambda
我正在尝试在 AWS 上的 Lambda 函数中使用 Python 拼写检查库 Pyenchant。Pyenchant 是 C libenchant 库的包装器,而该库又依赖于来自 Aspell 等提供商的单词词典。
在 Lambda 上运行的 python 代码中,我能够成功导入已编译的 enchant 库以及 AWS Linux EC2 实例上的 C 库 (libenchant.so),并将输出复制到我的 Lambda 部署包。
然而,当 pyenchant 库在 Lambda 上运行时,它无法加载任何需要工作的单词词典。然后我使用以下命令在 EC2 实例上安装了 Aspell:
yum install aspell-en enchant-aspell
Run Code Online (Sandbox Code Playgroud)
然后,我将以下附加 .so 文件复制到部署包的 /lib 文件夹中:
我很确定 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"
Run Code Online (Sandbox Code Playgroud)
以下是调用 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
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,import enchant工作正常,但它找不到任何字典文件。
我真的被这个问题困扰了,我花了 6 个小时的大部分时间试图弄清楚如何让它发挥作用。在此先感谢您的帮助。
好吧,对于遇到这个问题的其他人(可能没有人......)来说,事实证明不可能在 Lambda 上使用这个包。与没有正确的基础设施来加载多层共享对象资源有关。最后我只在 EC2 上使用了 Flask Web 服务器,并且运行良好。