我想使用weave来编译与python Anaconda 64一起使用的C代码,但是失败了。遇到各种问题。下面的代码

import numpy
import weave
from weave import converters

def my_sum(a):
    n=int(len(a))
    code="""
    int i;
    long int counter;
    counter =0;
    for(i=0;i<n;i++)
    {
        counter=counter+a(i);
    }
    return_val=counter;
    """

    err=weave.inline(code,['a','n'],type_converters=converters.blitz,compiler='gcc')

a = numpy.array(range(60000))
my_sum(a)


1)似乎anaconda不提供带符号的msvcr90.dll。看到
    python27.def "Symbol table not found" when trying weave

2)虚伪会发生一些不匹配

In file included from C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/math.h:36,
                 from C:\ProgramData\Anaconda3\envs\Python27\include/pyport.h:325,
                 from C:\ProgramData\Anaconda3\envs\Python27\include/Python.h:61,
                 from C:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\scxx\object.h:11,
                 from C:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\scxx\weave_imp.cpp:7:
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/cmath:1121:11: error: '::hypot' has not been declared
   using ::hypot;
           ^~~~~In file included from C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/math.h:36,
                 from C:\ProgramData\Anaconda3\envs\Python27\include/pyport.h:325,
                 from C:\ProgramData\Anaconda3\envs\Python27\include/Python.h:61,


pyconfig.h文件中将hypot重新定义为_hypot。解决此错误的方法是在cmath的1121行中注释掉hypot

1121 // using ::hypot;


最终可以编译组织代码,但是错误ImportError:DLL加载失败:%1不是有效的Win32应用程序。请参阅下面的日志文件
MINGW64和TDM CODEBLOCKS的两个gcc编译器都发生这种情况

不知道这是什么原因

running build_ext
running build_src
build_src
building extension "sc_98b95ee93f2252921d5f3dc059bd1293105" sources
build_src: building npy-pkg config files
Skip building import library: "C:\ProgramData\Anaconda3\envs\Python27\libs\libpython27.a" exists
new_compiler returns numpy.distutils.mingw32ccompiler.Mingw32CCompiler
customize Mingw32CCompiler
customize Mingw32CCompiler using build_ext
********************************************************************************
numpy.distutils.mingw32ccompiler.Mingw32CCompiler
linker_exe    = ['gcc', '-g']
compiler_so   = ['gcc', '-g', '-DDEBUG', '-DMS_WIN64', '-O0', '-Wall', '-Wstrict-prototypes']
archiver      = ['ar', '-cr']
preprocessor  = None
linker_so     = ['gcc', '-g', '-shared']
compiler_cxx  = ['g++']
ranlib        = None
compiler      = ['gcc', '-g', '-DDEBUG', '-DMS_WIN64', '-O0', '-Wall']
libraries     = []
library_dirs  = ['C:\\ProgramData\\Anaconda3\\envs\\Python27\\libs', 'C:\\ProgramData\\Anaconda3\\envs\\Python27\\PCbuild\\amd64', 'C:\\ProgramData\\Anaconda3\\envs\\Python27\\PC\\VS9.0\\amd64']
include_dirs  = ['C:\\ProgramData\\Anaconda3\\envs\\Python27\\include', 'C:\\ProgramData\\Anaconda3\\envs\\Python27\\PC']
********************************************************************************
Skip building import library: "C:\ProgramData\Anaconda3\envs\Python27\libs\libpython27.a" exists
new_compiler returns numpy.distutils.mingw32ccompiler.Mingw32CCompiler
customize Mingw32CCompiler
customize Mingw32CCompiler using build_ext
********************************************************************************
numpy.distutils.mingw32ccompiler.Mingw32CCompiler
linker_exe    = ['gcc', '-g']
compiler_so   = ['gcc', '-g', '-DDEBUG', '-DMS_WIN64', '-O0', '-Wall']
archiver      = ['ar', '-cr']
preprocessor  = None
linker_so     = ['gcc', '-g', '-shared']
compiler_cxx  = ['g++']
ranlib        = None
compiler      = ['gcc', '-g', '-DDEBUG', '-DMS_WIN64', '-O0', '-Wall']
libraries     = []
library_dirs  = ['C:\\ProgramData\\Anaconda3\\envs\\Python27\\libs', 'C:\\ProgramData\\Anaconda3\\envs\\Python27\\PCbuild\\amd64', 'C:\\ProgramData\\Anaconda3\\envs\\Python27\\PC\\VS9.0\\amd64']
include_dirs  = ['C:\\ProgramData\\Anaconda3\\envs\\Python27\\include', 'C:\\ProgramData\\Anaconda3\\envs\\Python27\\PC']
********************************************************************************
building 'sc_98b95ee93f2252921d5f3dc059bd1293105' extension
compiling C++ sources
C compiler: g++ -g -DDEBUG -DMS_WIN64 -O0 -Wall

compile options: '-D__MSVCRT_VERSION__=0x1500 -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\scxx -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\blitz -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\numpy\core\include -IC:\ProgramData\Anaconda3\envs\Python27\include -IC:\ProgramData\Anaconda3\envs\Python27\PC -c'
g++ -g -DDEBUG -DMS_WIN64 -O0 -Wall -D__MSVCRT_VERSION__=0x1500 -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\scxx -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\blitz -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\numpy\core\include -IC:\ProgramData\Anaconda3\envs\Python27\include -IC:\ProgramData\Anaconda3\envs\Python27\PC -c c:\users\krzysz~1.faj\appdata\local\temp\1\krzysztof.fajst\python27_compiled\sc_98b95ee93f2252921d5f3dc059bd1293105.cpp -o c:\users\krzysz~1.faj\appdata\local\temp\1\weave-krzysztof.fajst-iaqybo\python27_intermediate\compiler_44169fb3373e7089eb20b6ac951fd048\Release\users\krzysz~1.faj\appdata\local\temp\1\krzysztof.fajst\python27_compiled\sc_98b95ee93f2252921d5f3dc059bd1293105.og++ -g -DDEBUG -DMS_WIN64 -O0 -Wall -D__MSVCRT_VERSION__=0x1500 -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\scxx -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\blitz -IC:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\numpy\core\include -IC:\ProgramData\Anaconda3\envs\Python27\include -IC:\ProgramData\Anaconda3\envs\Python27\PC -c C:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\scxx\weave_imp.cpp -o c:\users\krzysz~1.faj\appdata\local\temp\1\weave-krzysztof.fajst-iaqybo\python27_intermediate\compiler_44169fb3373e7089eb20b6ac951fd048\Release\programdata\anaconda3\envs\python27\lib\site-packages\weave\scxx\weave_imp.o

exec_command(['g++', '-g', '-DDEBUG', '-DMS_WIN64', '-O0', '-Wall', '-D__MSVCRT_VERSION__=0x1500', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\lib\\site-packages\\weave', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\lib\\site-packages\\weave\\scxx', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\lib\\site-packages\\weave\\blitz', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\lib\\site-packages\\numpy\\core\\include', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\include', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\PC', '-c', 'C:\\ProgramData\\Anaconda3\\envs\\Python27\\lib\\site-packages\\weave\\scxx\\weave_imp.cpp', '-o', 'c:\\users\\krzysz~1.faj\\appdata\\local\\temp\\1\\weave-krzysztof.fajst-iaqybo\\python27_intermediate\\compiler_44169fb3373e7089eb20b6ac951fd048\\Release\\programdata\\anaconda3\\envs\\python27\\lib\\site-packages\\weave\\scxx\\weave_imp.o'],)exec_command(['g++', '-g', '-DDEBUG', '-DMS_WIN64', '-O0', '-Wall', '-D__MSVCRT_VERSION__=0x1500', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\lib\\site-packages\\weave', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\lib\\site-packages\\weave\\scxx', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\lib\\site-packages\\weave\\blitz', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\lib\\site-packages\\numpy\\core\\include', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\include', '-IC:\\ProgramData\\Anaconda3\\envs\\Python27\\PC', '-c', 'c:\\users\\krzysz~1.faj\\appdata\\local\\temp\\1\\krzysztof.fajst\\python27_compiled\\sc_98b95ee93f2252921d5f3dc059bd1293105.cpp', '-o', 'c:\\users\\krzysz~1.faj\\appdata\\local\\temp\\1\\weave-krzysztof.fajst-iaqybo\\python27_intermediate\\compiler_44169fb3373e7089eb20b6ac951fd048\\Release\\users\\krzysz~1.faj\\appdata\\local\\temp\\1\\krzysztof.fajst\\python27_compiled\\sc_98b95ee93f2252921d5f3dc059bd1293105.o'],)

Retaining cwd: c:\users\krzysz~1.faj\appdata\local\temp\1\krzysztof.fajst\python27_compiledRetaining cwd: c:\users\krzysz~1.faj\appdata\local\temp\1\krzysztof.fajst\python27_compiled

_preserve_environment([])_preserve_environment([])

_update_environment(...)_update_environment(...)

_update_environment(...)
_update_environment(...)
g++ -g -shared c:\users\krzysz~1.faj\appdata\local\temp\1\weave-krzysztof.fajst-iaqybo\python27_intermediate\compiler_44169fb3373e7089eb20b6ac951fd048\Release\users\krzysz~1.faj\appdata\local\temp\1\krzysztof.fajst\python27_compiled\sc_98b95ee93f2252921d5f3dc059bd1293105.o c:\users\krzysz~1.faj\appdata\local\temp\1\weave-krzysztof.fajst-iaqybo\python27_intermediate\compiler_44169fb3373e7089eb20b6ac951fd048\Release\programdata\anaconda3\envs\python27\lib\site-packages\weave\scxx\weave_imp.o -LC:\ProgramData\Anaconda3\envs\Python27\libs -LC:\ProgramData\Anaconda3\envs\Python27\PCbuild\amd64 -LC:\ProgramData\Anaconda3\envs\Python27\PC\VS9.0\amd64 -lpython27 -lmsvcr90 -o c:\users\krzysz~1.faj\appdata\local\temp\1\krzysztof.fajst\python27_compiled\sc_98b95ee93f2252921d5f3dc059bd1293105.pyd
exec_command(['g++', '-g', '-shared', 'c:\\users\\krzysz~1.faj\\appdata\\local\\temp\\1\\weave-krzysztof.fajst-iaqybo\\python27_intermediate\\compiler_44169fb3373e7089eb20b6ac951fd048\\Release\\users\\krzysz~1.faj\\appdata\\local\\temp\\1\\krzysztof.fajst\\python27_compiled\\sc_98b95ee93f2252921d5f3dc059bd1293105.o', 'c:\\users\\krzysz~1.faj\\appdata\\local\\temp\\1\\weave-krzysztof.fajst-iaqybo\\python27_intermediate\\compiler_44169fb3373e7089eb20b6ac951fd048\\Release\\programdata\\anaconda3\\envs\\python27\\lib\\site-packages\\weave\\scxx\\weave_imp.o', '-LC:\\ProgramData\\Anaconda3\\envs\\Python27\\libs', '-LC:\\ProgramData\\Anaconda3\\envs\\Python27\\PCbuild\\amd64', '-LC:\\ProgramData\\Anaconda3\\envs\\Python27\\PC\\VS9.0\\amd64', '-lpython27', '-lmsvcr90', '-o', 'c:\\users\\krzysz~1.faj\\appdata\\local\\temp\\1\\krzysztof.fajst\\python27_compiled\\sc_98b95ee93f2252921d5f3dc059bd1293105.pyd'],)
Retaining cwd: c:\users\krzysz~1.faj\appdata\local\temp\1\krzysztof.fajst\python27_compiled
_preserve_environment([])
_update_environment(...)
_update_environment(...)
Traceback (most recent call last):

  File "<ipython-input-2-fc41d47f58a9>", line 20, in <module>
    my_sum(a)

  File "<ipython-input-2-fc41d47f58a9>", line 14, in my_sum
    err=weave.inline(code,['a','n'],type_converters=converters.blitz,compiler='gcc')

  File "C:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\inline_tools.py", line 366, in inline
    **kw)

  File "C:\ProgramData\Anaconda3\envs\Python27\lib\site-packages\weave\inline_tools.py", line 502, in compile_function
    exec('import ' + module_name)

  File "<string>", line 1, in <module>

ImportError: DLL load failed: %1 is not a valid Win32 application.

最佳答案

经过更多调查后发现,C编译器gcc不应与Win 10下的Python Anaconda 64位一起使用,而应与MS Visual Studio 2017中的cl C编译器一起使用。
安装MS Visual Studio之后,有必要启动正确的开发人员命令提示符(对于64位Anaconda为x64或对于32位Anaconda为x86),并从此提示符启动例如IDE(Spyder)。在这种情况下,所有路径均已正确设置为将cl用作C编译器,例如用于Cython或Weave。

如果使用Linux,则gcc是默认的编译器,安装后可以正常工作

关于python - weawve ImportError:DLL加载失败:%1不是有效的Win32应用程序Anaconda64 MINGW64,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54764514/

10-16 16:45