使用f2py后未生成扩展模块(* .so)

时间:2018-11-16 05:12:23

标签: python f2py

我使用Win7,并为蟒蛇安装了Anaconda,为fortran安装了gfortran(MINGW)。

使用f2py后,我找不到扩展模块文件。 我在以下链接中尝试了该示例:

Three ways to wrap

F2PY - Calling Fortran routines from Python

如果我在Anaconda提示符下键入以下内容,

<base> C:\Codes\F2PY> f2py -c --fcompiler=gfortran -m fib1 fib1.f

然后,生成fib1.cp36-win_amd64.pyd文件和fib1文件夹。 .libs文件夹位于fib文件夹下,并且libfib1.5W62YGKMVL6PD3QLPPSXTQ2OGA5LRIRQ.gfortran-win_amd64文件夹中存在.libs文件。

但是,未生成预期的扩展模块(fib1.so)。

有什么我想念的吗?

我已经检查了gfortran和python是否可以分别正常工作。

C:\Codes\F2PY>f2py -c --fcompiler=gfortran -m fib1 fib1.f   
C:\Codes\F2PY>call "D:\Anaconda\Scripts\\..\python.exe" "D:\Anaconda\Scripts\\f2py.py" -c --fcompiler=gfortran -m fib1 fib1.f
Unknown vendor: "gfortran"
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, 
build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, 
build commands --fcompiler options
running build_src
build_src
building extension "fib1" sources
f2py options: []
f2py:>C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\src.win-amd64-3.6\fib1module.c
creating C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\src.win-amd64-3.6
Reading fortran codes...
        Reading file 'fib1.f' (format:fix,strict)
Post-processing...
        Block: fib1
                    Block: fib
Post-processing (stage 2)...
Building modules...
       Building module "fib1"...
            Constructing wrapper function "fib"...
              fib(a,[n])
    Wrote C/API module "fib1" to file "C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\src.win-amd64-3.6\fib1module.c"
  adding 'C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\src.win-amd64-3.6\fortranobject.c' to sources.
  adding 'C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\src.win-amd64-3.6' to include_dirs.
copying D:\Anaconda\lib\site-packages\numpy\f2py\src\fortranobject.c -> C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\src.win-amd64-3.6
copying D:\Anaconda\lib\site-packages\numpy\f2py\src\fortranobject.h -> C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\src.win-amd64-3.6
build_src: building npy-pkg config files
running build_ext
No module named 'numpy.distutils._msvccompiler' in numpy.distutils; trying from distutils
customize MSVCCompiler
customize MSVCCompiler using build_ext
customize Gnu95FCompiler
Found executable C:\MinGW\bin\gfortran.exe
customize Gnu95FCompiler using build_ext
building 'fib1' extension
compiling C sources
creating C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\Release\Users
creating C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\Release\Users\6829396
creating C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\Release\Users\6829396\AppData
creating C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\Release\Users\6829396\AppData\Local
creating C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\Release\Users\6829396\AppData\Local\Temp
creating C:\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_\Release\Users\6829396\AppData\Local\Temp\tmpkzx_h7w_

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:c:\mingw\lib\gcc\mingw32\6.3.0 /LIBPATH:c:\mingw\lib /LIBPATH:D:\Anaconda\libs /LIBPATH:D:\Anaconda\PCbuild\amd64 /LIBPATH:D:\Anaconda\libs /LIBPATH:D:\Anaconda\PCbuild\amd64 /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\ATLMFC\lib\x64" /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\lib\x64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\ucrt\x64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x64" /EXPORT:PyInit_fib1 C:\Users\6829396\AppData\Local\Temp\tmpo3wc2rbs\Release\Users\6829396\AppData\Local\Temp\tmpo3wc2rbs\src.win-amd64-3.6\fib1module.obj C:\Users\6829396\AppData\Local\Temp\tmpo3wc2rbs\Release\Users\6829396\AppData\Local\Temp\tmpo3wc2rbs\src.win-amd64-3.6\fortranobject.obj C:\Users\6829396\AppData\Local\Temp\tmpo3wc2rbs\Release\libfib1.5W62YGKMVL6PD3QLPPSXTQ2OGA5LRIRQ.gfortran-win_amd64.lib /OUT:.\fib1.cp36-win_amd64.pyd /IMPLIB:C:\Users\6829396\AppData\Local\Temp\tmpo3wc2rbs\Release\Users\6829396\AppData\Local\Temp\tmpo3wc2rbs\src.win-amd64-3.6\fib1.cp36-win_amd64.lib copying C:\Users\6829396\AppData\Local\Temp\tmpo3wc2rbs\Release\.libs\libfib1.5W62YGKMVL6PD3QLPPSXTQ2OGA5LRIRQ.gfortran-win_amd64.dll -> .\fib1\.libs Removing build directory C:\Users\6829396\AppData\Local\Temp\tmpo3wc2rbs

1 个答案:

答案 0 :(得分:0)

我还使用Win 7 + Anaconda + gfortran(MINGW),并且尝试包装包含在subroutine distan(x,nx,nd,d)文件中的sdistan.f90时遇到相同的问题。

命令python -m numpy.f2py -c -m sdistan sdistan.f90生成了sdistan.cp37-win_amd64.pyd文件而不是*.so模块。

depends.exe分析sdistan.cp37-win_amd64.pyd文件后,我发现存在两个未解决的外部依赖项:

(1) f2py 放置在libsdistan.LD66OEXBPEJ74HMF6SZDLFFCVSPSTQVW.gfortran-win_amd64.dll文件夹中的sdistan/.libs DLL文件。

(2)和python3.dll

我将这两个文件(第二个文件来自C:/Anaconda3/python3.dll)复制到了我的工作目录中,问题已解决。

现在我可以import sdistan,并且可以将我的函数用作output=sdistan.distan(输入参数)。

我希望这可以帮助那些发现相同问题的人。如果有人可以解释并纠正 f2py 的这种(奇怪/缺失)行为,那就太好了。

感谢大家为堆栈溢出做出贡献!