在为python cextension编写安装文件时,如何指定动态库位置?

2024-09-30 22:25:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为python编写一个c扩展,我正在使用一个库(FFTW),其中包含一些动态库文件(.dll)。因此,我编写了一些c代码和一个setup.py文件来安装它,当我运行安装文件时,一切都很顺利,我的c扩展名将成功安装。但当我尝试使用新模块时,我会遇到以下错误:

ImportError: DLL load failed: The specified module could not be found.

所以我假设它是指FFTW库的dll文件。如果我将dll文件复制到我的C:/windows/system32中,我的代码将不会出现任何错误。但我不希望这样做,而是将dll文件与我的代码放在一起。 根据python documentation编写设置脚本:

You can also specify the libraries to link against when building your extension, and the directories to search for those libraries. The libraries option is a list of libraries to link against, library_dirs is a list of directories to search for libraries at link-time, and runtime_library_dirs is a list of directories to search for shared (dynamically loaded) libraries at run-time.

但是当我使用runtime\u library\u dirs选项并试图通过运行setup.py来安装扩展时,我将遇到以下错误

warning: I don't know what to do with 'runtime_library_dirs': ['./c-extension/bin'] error: don't know how to set runtime library search path for MSVC

我正在使用python 3.6

下面是我的setup.py代码示例:

import numpy
from distutils.core import setup, Extension

def main():
    setup(name="simple_ext",
          version="1.0.0",
          description="Python interface for the simple C extension library function",
          author="My Name",
          author_email="my_email@email.com",
          data_files=[('./c-extension/bin', ['libfftw3-3.dll','libfftw3l-3.dll','libfftw3f-3.dll'])],
          ext_modules=[Extension("simple_ext", ["simple_ext.cpp"],
                  include_dirs=[numpy.get_include(), './c-extension/include']),
                  runtime_library_dirs=['./c-extension/bin'],
                  library_dirs=['./c-extension/lib'],
                  libraries = ['libfftw3-3','libfftw3l-3','libfftw3f-3']])

if __name__ == "__main__":
    main()

那么,有没有人知道什么地方出了问题,我该怎么做才能解决

编辑:

因此,根据帕特里克·波拉科维奇的评论,我设法解决了这个问题。问题是当我使用数据文件时,如下所示:

data_files=[('./c-extension/bin', ['libfftw3-3.dll','libfftw3l-3.dll','libfftw3f-3.dll'])],

有一个隐藏的错误消息,我错过了,显然也被编译器忽略了

error: can't copy 'libfftw3-3.dll': doesn't exist or not a regular file

但当我把它改成下面的表格时:

data_files=[('', ['./c-extension/bin/libfftw3-3.dll','./c-extension/bin/libfftw3l-3.dll','./c-extension/bin/libfftw3f-3.dll'])],

安装程序能够找到我的共享库文件,并显示以下消息:

copying c-extension\bin\libfftw3-3.dll -> C:\Users\user\.conda\envs\newEnv\

copying c-extension\bin\libfftw3l-3.dll -> C:\Users\user\.conda\envs\newEnv\

copying c-extension\bin\libfftw3f-3.dll -> C:\Users\user\.conda\envs\newEnv\

所以我可以用它作为一个临时解决方案。但我仍然不知道我使用的第一种形式的数据文件选项有什么问题(在我看来这是合法的),更重要的是,我不明白使用运行库\u目录选项有什么问题


Tags: 文件to代码librariesforbinsetupextension
1条回答
网友
1楼 · 发布于 2024-09-30 22:25:18

是的,在Windows上runtime_library_dirs不工作,将导致MSVC出错

在最初的问题中,您错误地使用了data_files,正如在编写设置脚本时的relevant part of the ^{} docs一样,请注意下面的句子

Each (directory, files) pair in the sequence specifies the installation directory and the files to install there.

您的DLL位于./c-extension/bin,因此需要在每个DLL的名称前面加上该相对路径。同一页的以下内容简要说明了目录

The directory should be a relative path. It is interpreted relative to the installation prefix (Python’s sys.prefix for system installations; site.USER_BASE for user installations).

也就是说,在文件中指定的文件将被复制到C:\Users\user\.conda\envs\newEnv\directory。因此,将./c-extension/bin传递到目录也是一个错误

相关问题 更多 >