我使用Win7,为pyran安装了Anaconda,为fortran安装了gfortran(MINGW)。
使用f2py后,我找不到扩展模块文件。我在以下链接中尝试了这个示例:
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
)。
我错过了什么吗?
我已经检查过fortran和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
我也使用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
模块。
在用sdistan.cp37-win_amd64.pyd
分析depends.exe文件后,我发现有两个未解析的外部依赖项:
(1)f2x放置在libsdistan.LD66OEXBPEJ74HMF6SZDLFFCVSPSTQVW.gfortran-win_amd64.dll
文件夹中的sdistan/.libs
DLL文件。
(2)和python3.dll
我将这两个文件(C:/Anaconda3/python3.dll
中的第二个)复制到我的工作目录中,问题解决了。
现在我可以import sdistan
,我可以使用我的函数output=sdistan.distan
(输入参数)。
我希望这可以帮助那些发现同样问题的人。如果有人可以解释并纠正f2py的这种(奇怪/错过)行为,那将是很好的。
感谢大家为Stack Overflow做出贡献!!