使用Python 3的AWS Lambda出现sqlite3错误 [英] sqlite3 error on AWS lambda with Python 3

查看:99
本文介绍了使用Python 3的AWS Lambda出现sqlite3错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建python 3.6 AWS Lambda部署软件包,并且遇到SQLite的问题.

I am building a python 3.6 AWS Lambda deploy package and was facing an issue with SQLite.

在我的代码中,我使用的是nltk,其中一个文件中有一个import sqlite3.

In my code I am using nltk which has a import sqlite3 in one of the files.

到目前为止采取的步骤:

Steps taken till now:

  1. 部署程序包在根目录中仅包含我正在使用的python模块.我得到了错误: Unable to import module 'my_program': No module named '_sqlite3'

  1. Deployment package has only python modules that I am using in the root. I get the error: Unable to import module 'my_program': No module named '_sqlite3'

/home/my_username/anaconda2/envs/py3k/lib/python3.6/lib-dynload/_sqlite3.so中的_sqlite3.so添加到程序包的根目录中.然后我的错误更改为:

Added the _sqlite3.so from /home/my_username/anaconda2/envs/py3k/lib/python3.6/lib-dynload/_sqlite3.so into package root. Then my error changed to:

Unable to import module 'my_program': dynamic module does not define module export function (PyInit__sqlite3)

sqlite.org中的SQLite预编译二进制文件添加到了程序包的根目录中,但在第二点我仍然收到错误消息.

Added the SQLite precompiled binaries from sqlite.org to the root of my package but I still get the error as point #2.

我的设置:Ubuntu 16.04python3 virtual env

AWS lambda env:python3

AWS lambda env: python3

如何解决此问题?

推荐答案

根据您对NLTK的处理方式,我可能已经找到了解决方案.

Depending on what you're doing with NLTK, I may have found a solution.

基本nltk模块导入了很多依赖关系,其中许多依赖关系未由其功能集的大部分使用.在我的用例中,我仅使用nltk.sent_tokenize,即使sqlite3被作为依赖项导入,它也没有对sqlite3的功能依赖.

The base nltk module imports a lot of dependencies, many of which are not used by substantial portions of its feature set. In my use case, I'm only using the nltk.sent_tokenize, which carries no functional dependency on sqlite3 even though sqlite3 gets imported as a dependency.

通过更改,我能够使我的代码在AWS Lambda上运行

I was able to get my code working on AWS Lambda by changing

import nltk

import imp
import sys
sys.modules["sqlite"] = imp.new_module("sqlite")
sys.modules["sqlite3.dbapi2"] = imp.new_module("sqlite.dbapi2")
import nltk

这会动态为sqlitesqlite.dbapi2创建空模块.当nltk.corpus.reader.panlex_lite尝试导入sqlite时,它将获得我们的空模块,而不是标准库版本.这意味着导入将成功,但也意味着当nltk尝试使用sqlite模块时,它将失败.

This dynamically creates empty modules for sqlite and sqlite.dbapi2. When nltk.corpus.reader.panlex_lite tries to import sqlite, it will get our empty module instead of the standard library version. That means the import will succeed, but it also means that when nltk tries to use the sqlite module it will fail.

如果您正在使用实际上依赖于sqlite的任何功能,恐怕我无能为力.但是,如果您尝试使用其他nltk功能,并且只需要解决sqlite的不足,那么此技术可能会起作用.

If you're using any functionality that actually depends on sqlite, I'm afraid I can't help. But if you're trying to use other nltk functionality and just need to get around the lack of sqlite, this technique might work.

这篇关于使用Python 3的AWS Lambda出现sqlite3错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆