0

我正在尝试将我的 python 代码转换为 dll,在下面的代码中,ffi.embedding_init_code我可以导入我用 pip 或 conda 安装的包,如 cv2、numpy、pil 等,但我创建了 python 文件 my_tools.py,这在访问时出错dll。“ModuleNotfoundError:没有名为‘my_tools’的模块”

import re
import cffi
ffi = cffi.FFI()
with open('plugin.h') as f:
    include = f.read()

ffi.embedding_api(include)

ffi.set_source("my_plugin", 
        re.sub(r'^extern(?=\s)', 'CFFI_DLLEXPORT', include, flags=re.M))

ffi.embedding_init_code("""
    from my_plugin import ffi, lib
    import keras_ocr
    import my_tools # as m_tools
    import logging
    import sys
    import cv2
    import numpy as np
    from PIL import Image
    import io
    import base64

    @ffi.def_extern()
    def hello(out_result):
        out_result=ffi.string(out_result)
        print("hello python="+str(out_result))
        return 0
""")
ffi.cdef("""
    char *strdup(const char *);
""")
ffi.compile(target="plugin-1.5.*", verbose=True)

下面是我的plugin.h

extern int hello(char* out_result);

如何在这里导入我自己创建的文件。

4

1 回答 1

1

没有千篇一律的答案,但一种快速入门的方法是将其添加为第一行 embedding_init_code

import sys; sys.path.insert(0, "/path/containing/the/python/files")
于 2020-09-15T09:02:39.467 回答