如何使用ctypes.util.find_library导入AWS Lambda(python)中的.so库? [英] How to use ctypes.util.find_library to import .so libraries in AWS lambda (python)?

查看:154
本文介绍了如何使用ctypes.util.find_library导入AWS Lambda(python)中的.so库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Lambda上使用的(OCRMYPDF)python软件包需要leptonica库 liblept.so.5 .在隔离导入代码时,我发现问题出在 find_library('lept').打印结果返回None.

A python package I'm using (OCRMYPDF) on Lambda needs the leptonica library liblept.so.5. On isolating the import code I found the issue is with find_library('lept'). Printing the result returns None.

from ctypes.util import find_library
def lambda_handler(event, context):
    liblept=find_library('lept')
    print("liblept:%s"%liblept)

我正在使用的python包需要许多本机编译的依赖项.我正在尝试使用lambda图层导入这些图片.

The python package I'm using needs many native compiled dependencies. I'm trying to import these using lambda layers.

/opt/
  /opt/bin/
  /opt/lib/
    /opt/lib/liblept.so.5
  /opt/tesseract

我可以使用CDLL(下面的代码)访问文件.但是我不想重写程序包,并用CDLL替换每个find_library().可以为find_library设置导入目录吗?

I'm able to access the file using CDLL (code below). But I don't want to rewrite the package and replace every find_library() with CDLL. Is it possible to set the import directory for find_library?

liblept=CDLL("/opt/lib/liblept.so.5") # found
print("liblept:%s"%liblept)

我的图层代码有效

我使用了docker镜像来构建图层./opt/bin中依赖于leptonica的文件正在运行(tesseract正常运行,还测试了OCR).

My layer code works

I used a docker image to build layer. The files in /opt/bin that depend on leptonica are working (tesseract runs properly, tested OCR as well).

logging.info(os.system("tesseract --version"))

输出

START RequestId: d826d36c-4ce9-4b67-b501-8c9042edcf80 Version: $LATEST
tesseract 4.1.0
 leptonica-1.78.0
  libgif 5.1.4 : libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.2.49 : libtiff 4.0.3 : zlib 1.2.8 : libwebp 0.3.0
 Found AVX
 Found SSE
END RequestId: d826d36c-4ce9-4b67-b501-8c9042edcf80

推荐答案

在Python:3.7 AWS lambda环境中测试:

Tested on Python:3.7 AWS lambda environment:

您需要将liblept.so(只需重命名liblept.so.5)添加到lambda包的/lib文件夹中或图层的/opt/lib中.该文件必须称为liblept.so,因为find_library仅查找".so"文件,而不查找".so.5"文件:

You need to add liblept.so (just rename liblept.so.5) to the /lib folder of your lambda package or in /opt/lib of your layer. The file must be called liblept.so because find_library only looks for ".so" files, not ".so.5" files:

在python文档中: https://docs.python.org/3/library/ctypes.html

From the python doc: https://docs.python.org/3/library/ctypes.html

ctypes.util.find_library(name)
Try to find a library and return a pathname. name is the library name without any prefix like lib, suffix like .so, .dylib or version number (this is the form used for the posix linker option -l). If no library can be found, returns None.

仅添加"liblept.so"时,链接器会抱怨找不到"liblept.so.5",因此我也将"liblept.so.5"添加到了lib文件夹中.

When only adding "liblept.so", the linker complains that it cannot find "liblept.so.5", so I also added "liblept.so.5" to the lib folder.

也许其他人可以介入并找到不使用重复文件的解决方案.

Maybe someone else can pitch in and find a solution that does not use a duplicate file.

AWS lambda将通过LD_LIBRARY_PATH自动提供/opt/lib/lib中的任何文件.

AWS lambda will automatically make any file in /opt/lib or /lib available via LD_LIBRARY_PATH.

在Python 3.8上,您可能还需要根据该线程包括ldobjdump: https://forums.aws.amazon.com/thread.jspa?threadID=313506 ,尽管我尚未对此进行测试.

On Python 3.8 you may also need to include ld and objdump, as per this thread: https://forums.aws.amazon.com/thread.jspa?threadID=313506, altough I have not tested that.

这篇关于如何使用ctypes.util.find_library导入AWS Lambda(python)中的.so库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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