Python cffi嵌入\初始化\代码导入自定义py文件
我正在尝试将我的python代码转换为dll,在下面的Python cffi嵌入\初始化\代码导入自定义py文件,python,python-cffi,cffi,Python,Python Cffi,Cffi,我正在尝试将我的python代码转换为dll,在下面的ffi下的代码中。Embedded_init_code我可以导入我用pip或conda安装的包,如cv2、numpy、pil等,但我创建了python文件my_tools.py,这在访问dll时会出错“ModuleNotfoundError:没有名为‘我的工具’的模块” import re import cffi ffi = cffi.FFI() with open('plugin.h') as f: include = f.read
ffi下的代码中。Embedded_init_code
我可以导入我用pip或conda安装的包,如cv2、numpy、pil等,但我创建了python文件my_tools.py,这在访问dll时会出错“ModuleNotfoundError:没有名为‘我的工具’的模块”
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)
下面是我的插件
extern int hello(char* out_result);
如何在此处导入我自己创建的文件。没有一个适合所有人的答案,但快速入门的方法是将其添加为
嵌入初始代码
:
import sys; sys.path.insert(0, "/path/containing/the/python/files")
谢谢你的回答,我在这里也看到了你的答案,编译器是从另一个位置运行的,我可以添加目录来查看ffi对象吗?还有一个问题,我可以导入同一目录下的文件,但不能导入同一目录下的目录。好的,我添加了类似sys.path.insert(0,“/path/containing/the/python/files/directory”),它起了作用