使用Cygwin中的Ctypes将DLL加载到Python中的奇怪问题

时间:2017-01-27 23:39:15

标签: python ctypes

我已经在这几个小时了。我有一个简单的hello-world .dll在Cygwin和Clion上使用gcc编译。

我有一个Python脚本从位于Cygwin目录中的Pycharm中的anaconda安装(即解释器)运行。

.dll文件名是" cygextension_test_c.dll。"

我将.dll移动到python项目目录,并运行以下代码,下面列出了各种配置(所有这些都是过去十年中有关此主题的问题的各种解决方案,我今天下午可以找到 - 我正在使用以下website来发送ndarray到C代码):

import numpy as N
from numpy.ctypeslib import load_library
from numpyctypes import c_ndarray
import ctypes
import os

print(ctypes.windll.kernel32);

mydll = load_library('cygextension_test_c.dll',r'C:\cygwin64\home\...\extension_test_c\cmake-build-debug');

mydll = ctypes.WinDLL(r'same path, essentially--this time pointing to working directory\cygextension_test_c.dll')

mydll = ctypes.CDLL(r'ditto')

mydll = ctypes.cdll.LoadLibrary('...')

mydll = ctypes.windl.LoadLibrary('...')

myarray = N.zeros((3,13),dtype=N.double)
c_myarray = c_ndarray(myarray,dtype=N.double,ndim=2)

我还介入了代码,并注意到路径和文件正在被访问。如果我将路径更改为不正确的路径(或文件名等),我会在链中更早出现错误。这在某种程度上与.dll本身有关。但是,几乎所有的问题都得到了(显然)相同的错误,并且它与它们的实际.dll无关。他们通过改变用ctypes导入.dll的方式来获得神奇的解决方案。

所以,再次,我在一台(显然)Windows计算机上的Cygwin目录中的pycharm上使用windows anaconda install(python 3.5)。在Clion中使用gcc编译c ++代码,并在CMakeLists文件中进行以下设置(根据上面链接中的说明):

cmake_minimum_required(VERSION 3.6)
project(extension_test_c)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -ansi -pedantic -Wall -pedantic -std=c++0x")

set(SOURCE_FILES extension_test_c.cpp extension_test_c.h ndarray.h)
add_library(extension_test_c SHARED ${SOURCE_FILES})

我在extension_test_c.cpp中编译的代码只是一个函数,它打印出stride数组在2-d numpy ndarray中的数字(这与链接中的代码有关),以及代码中的代码。标题更简单;但是,我已经注释掉了std行,因为我遇到了关于库是否访问其他库的早期问题的另一个答案(因为这可能导致ctypes导入失败)。

extension_test_c.cpp

#include "extension_test_c.h"

//#include <iostream>
#include "ndarray.h"

extern "C" {
int hello(numpyArray<double> array) {
    Ndarray<double,2> a(array);

    //std::cout << "shape 0: " << array.shape[0] << "; shape 1: " <<
    //          array.shape[1] << "; stride 0: " << array.strides[0]
    //          << "; strides 1: " << array.strides[1] << std::endl;
    return 1; 
}
}

头文件:

#ifndef EXTENSION_TEST_C_LIBRARY_H
#define EXTENSION_TEST_C_LIBRARY_H

#include "ndarray.h"
extern "C" {
int hello(numpyArray<double> array); 
}
#endif

ndarray.h可以在上面网站的链接中找到。

我不知道我做错了什么;我完全没有想法。以下是load_library的错误:

  Traceback (most recent call last):
  File "C:/cygwin64/home/chris/CygwinMachineLearning/Assignment 1/DecisionTree/extension_test_python/wrapper.py", line 9, in <module>
      mydll = load_library('cygextension_test_c.dll',r'C:\cygwin64\home\chris\CygwinMachineLearning\Assignment 1\DecisionTree\extension_test_c\cmake-build-debug');
  File "C:\Anaconda3\envs\tensorflow\lib\site-packages\numpy\ctypeslib.py", line 150, in load_library
      return ctypes.cdll[libpath]
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 422, in __getitem__
      return getattr(self, name)
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 417, in __getattr__
      dll = self._dlltype(name)
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 347, in __init__
      self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

ctypes.CDLL(...)的错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevd.py", line 974, in run 
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/cygwin64/home/chris/CygwinMachineLearning/Assignment 1/DecisionTree/extension_test_python/wrapper.py", line 9, in <module>
    mydll = ctypes.CDLL('cygextension_test_c.dll',r'C:\cygwin64\home\chris\CygwinMachineLearning\Assignment 1\DecisionTree\extension_test_c\cmake-build-debug');
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 347, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

而且,对于WinDLL,它是一样的。

对于cdll.LoadLibrary版本,它与不同的跟踪几乎相同(完全相同的最终错误):

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevd.py", line 974, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/cygwin64/home/chris/CygwinMachineLearning/Assignment 1/DecisionTree/extension_test_python/wrapper.py", line 9, in <module>
    mydll = ctypes.cdll.LoadLibrary(r'C:\cygwin64\home\chris\CygwinMachineLearning\Assignment 1\DecisionTree\extension_test_c\cmake-build-debug');
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 425, in LoadLibrary
    return self._dlltype(name)
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 347, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

2 个答案:

答案 0 :(得分:1)

对于ctypes.CDLL,库应该没有.dll扩展名。

所以

myDLL =  ctypes.CDLL("myPath/MyDLL")

不是

myDLL =  ctypes.CDLL("myPath/MyDLL.dll")

答案 1 :(得分:1)

经过数小时的尝试,对我而言,解决方案是在gcc链接器命令中添加“ -static”。来自Gcc Link Options

  

-静态

     

在支持动态链接的系统上,此选项将覆盖-pie和   防止链接共享库。在其他系统上,   选项无效。

[WinError 126]建议找不到某些依赖的dll。提示是相同的dll在不同的Windows系统上加载时。安装WSL可能也会使其正常工作,但是静态链接将其固定为没有WSL的计算机。