在Windows 10上使用f2py和Python 3.6编译fortran模块(Compile fortran module with f2py and Python 3.6 on Windows 10)

我正在尝试(并且失败)在Windows 10上使用f2py和Python 3.6编译fortran模块(特别是来自BGS的igrf12.f)。使用Anaconda 4.4.10安装Python。

我的设置:

Python 3.6.3 | Anaconda自定义(64位)| (默认情况下,2017年10月15日,03:27:45)[winv上的MSC v.1900 64位(AMD64)] Windows 10企业版(1703版) NumPy 1.14.0

我遵循了SciPy文档中的 f2py指示以及Dr.Michael Hirsch的非常有用的指南 。 Dr.Hirsch创建了pyigrf12模块,但是通过pip安装失败了,这最初引起了我对f2py的兴趣。

我将概述我正在使用的一些方法。 无论方法如何,我总是首先使用f2py igrf12.f -m pyigrf12 -h igrf12.pyf创建一个* .pyf签名文件,并适当地添加intent(in / out)属性。

方法1:使用C:\ MinGW和--compiler=mingw32 方法2:使用C:\ MinGW和--compiler=msvc 方法3:使用anaconda版本的mingw和--compiler=mingw32 方法4:使用anaconda版本的mingw和--compiler=msvc

方法1:

对于后台,我在C:\ MinGW有MinGW,我已将C:\ MinGW \ bin添加到我的用户Path。 不幸的是,我没有安装这个版本的MinGW(我从同事那里继承了这台电脑),所以我不知道它的来源。 gcc --version和gfortran --version是5.3.0。

我运行f2py -c igrf12.pyf igrf12.f --compiler=mingw32 。 此失败并显示以下错误消息:

Building import library (arch=AMD64): "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libpython36.a" (from C:\Users\Sholes\AppData\Local\Continuum\anaconda3\python36.dll) objdump.exe: C:\Users\Sholes\AppData\Local\Continuum\anaconda3\python36.dll: File format not recognized Traceback (most recent call last): File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py", line 28, in <module> main() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 648, in main run_compile() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 633, in run_compile setup(ext_modules=[ext]) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\core.py", line 169, in setup return old_setup(**new_attr) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build.py", line 47, in run old_build.run(self) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\command\build.py", line 135, in run self.run_command(cmd_name) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build_ext.py", line 117, in run force=self.force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\ccompiler.py", line 733, in new_compiler compiler = klass(None, dry_run, force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 104, in __init__ build_import_library() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 416, in build_import_library return _build_import_library_amd64() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 472, in _build_import_library_amd64 generate_def(dll_file, def_file) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 302, in generate_def raise ValueError("Symbol table not found") ValueError: Symbol table not found

问题似乎涉及从python36.dll构建libpython36.a

快速谷歌搜索后, github论坛上pywafo的建议是使用msvc编译器而不是mingw32,导致方法2。

方法2:对于后台,与我的msvc相关的文件是从C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64\ 。 不确定这是否有帮助。

我运行f2py -c igrf12.pyf igrf12.f --compiler=msvc 。 这会产生两个文件:

pyigrf12.cp36-win_amd64.pyd在我当前的工作目录中, libigrf12.BMWM6WD5Y3O3UTOEQITBXCIICXVMBEZS.gfortran-win_amd64.dll位于.\UNKNOWN\.libs\

当我尝试import pyigrf12 ,首先我收到ImportError: DLL load failed: The specified module could not be found. 使用Dependency Walker,我收到很多错误:

但最明显的问题与libigrf12.BMWM6WD5Y3O3UTOEQITBXCIICXVMBEZS.gfortran-win_amd64.dll 。 我将此libigrf12 dll文件与pyigrf12.cp36-win_amd64.pyd一起移动到我当前的工作目录。

现在,当我尝试import pyigrf12 ,我收到ImportError: DLL load failed: %1 is not a valid Win32 application. 。 一些stackoverflow帖子似乎表明这是一个32位DLL和64位Python之间的冲突的问题。 有人能提供这方面的见解吗? 经过更多的搜索,我决定尝试使用anaconda版本的mingw和libpython。

方法3:

我运行conda install mingw libpython ,它安装了mingw 4.7-1和libpython 2.1-py36_0。 我运行f2py -c igrf12.pyf igrf12.f --compiler=mingw32 ,它失败并显示以下错误消息:

Building msvcr library: "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libvcruntime140.a" (from C:\Users\Sholes\AppData\Local\Continuum\anaconda3\vcruntime140.dll) objdump.exe: C:\Users\Sholes\AppData\Local\Continuum\anaconda3\vcruntime140.dll: File format not recognized Traceback (most recent call last): File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py", line 28, in <module> main() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 648, in main run_compile() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 633, in run_compile setup(ext_modules=[ext]) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\core.py", line 169, in setup return old_setup(**new_attr) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build.py", line 47, in run old_build.run(self) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\command\build.py", line 135, in run self.run_command(cmd_name) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build_ext.py", line 117, in run force=self.force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\ccompiler.py", line 733, in new_compiler compiler = klass(None, dry_run, force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 107, in __init__ msvcr_success = build_msvcr_library() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 399, in build_msvcr_library generate_def(dll_file, def_file) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 302, in generate_def raise ValueError("Symbol table not found") ValueError: Symbol table not found

现在问题似乎与从vcruntime140.dll构建libvcruntime140.a有关。 objdump.exe无法识别dll文件格式。

方法4:

我的最后一次尝试是运行安装了anaconda版本的mingw的f2py -c igrf12.pyf igrf12.f --compiler=msvc 。 对于这种情况,gfortran失败了这个错误:

C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\gfortran.bat -Wall -g -Wall -g -shared ..\..\..\AppData\Local\Temp\tmpugo__0q9\Release\igrf12.o -Lc:\users\sholes\appdata\local\continuum\anaconda3\mingw\lib\gcc\x86_64-w64-mingw32\4.7.0 -LC:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs -LC:\Users\Sholes\AppData\Local\Continuum\anaconda3\PCbuild\amd64 -o C:\Users\Sholes\AppData\Local\Temp\tmpugo__0q9\Release\extra-dll\libigrf12.75XJA5DX6DTO7YIZ7X6ZHJYTRDCCYQYR.gfortran-win_amd64.dll -Wl,--allow-multiple-definition -Wl,--output-def,C:\Users\Sholes\AppData\Local\Temp\tmpugo__0q9\Release\libigrf12.75XJA5DX6DTO7YIZ7X6ZHJYTRDCCYQYR.gfortran-win_amd64.def -Wl,--export-all-symbols -Wl,--enable-auto-import -static -mlong-double-64 gfortran.exe: error: unrecognized command line option '-mlong-double-64'

此时,我只想知道是否可以使用我的设置和f2py创建fortran扩展。 我没有在Windows上编译C或Fortran扩展的任何后台,并且基于与Windows上的Python 3.6 scipy和numpy安装问题相关的所有问题,似乎这是一个没有简单解决方案的常见问题。

任何反馈或见解将不胜感激。

I'm trying (and failing) to compile a fortran module (specifically igrf12.f from the BGS) using f2py and Python 3.6 on Windows 10. Python was installed using Anaconda 4.4.10.

My setup:

Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] on win32 Windows 10 Enterprise (version 1703) NumPy 1.14.0

I followed f2py directions from the SciPy documentation and a very helpful guide from Dr.Michael Hirsch. Dr.Hirsch has created the pyigrf12 module, but installation through pip failed for me, which is what initially sparked my interest in f2py.

I will outline a few of the methods I am using. Regardless of the method, I always begin by creating a *.pyf signature file using f2py igrf12.f -m pyigrf12 -h igrf12.pyf and adding intent(in/out) attributes appropriately.

Method 1: Use C:\MinGW and --compiler=mingw32 Method 2: Use C:\MinGW and --compiler=msvc Method 3: use anaconda version of mingw and --compiler=mingw32 Method 4: use anaconda version of mingw and --compiler=msvc

Method 1:

For background, I have MinGW at C:\MinGW and I have added C:\MinGW\bin to my user Path. Unfortunately, I did not install this version of MinGW (I inherited this computer from a colleague), so I do not know where it was sourced. The gcc --version and gfortran --version is 5.3.0.

I run f2py -c igrf12.pyf igrf12.f --compiler=mingw32. This fails with this error message:

Building import library (arch=AMD64): "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libpython36.a" (from C:\Users\Sholes\AppData\Local\Continuum\anaconda3\python36.dll) objdump.exe: C:\Users\Sholes\AppData\Local\Continuum\anaconda3\python36.dll: File format not recognized Traceback (most recent call last): File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py", line 28, in <module> main() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 648, in main run_compile() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 633, in run_compile setup(ext_modules=[ext]) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\core.py", line 169, in setup return old_setup(**new_attr) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build.py", line 47, in run old_build.run(self) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\command\build.py", line 135, in run self.run_command(cmd_name) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build_ext.py", line 117, in run force=self.force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\ccompiler.py", line 733, in new_compiler compiler = klass(None, dry_run, force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 104, in __init__ build_import_library() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 416, in build_import_library return _build_import_library_amd64() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 472, in _build_import_library_amd64 generate_def(dll_file, def_file) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 302, in generate_def raise ValueError("Symbol table not found") ValueError: Symbol table not found

The issue seems to involve building libpython36.a from python36.dll.

After a quick google search, a suggestion on a github forum for pywafo was to use the msvc compiler instead of mingw32, leading to Method 2.

Method 2: For background, the files related to my msvc are being pulled from C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64\. Not sure if that's helpful.

I run f2py -c igrf12.pyf igrf12.f --compiler=msvc. This produces two files:

pyigrf12.cp36-win_amd64.pyd in my current working directory and, libigrf12.BMWM6WD5Y3O3UTOEQITBXCIICXVMBEZS.gfortran-win_amd64.dll in .\UNKNOWN\.libs\

When I try to import pyigrf12, first I receive ImportError: DLL load failed: The specified module could not be found. Using Dependency Walker, I receive a lot of errors:

but the most obvious one to address is related to the libigrf12.BMWM6WD5Y3O3UTOEQITBXCIICXVMBEZS.gfortran-win_amd64.dll. I move this libigrf12 dll file to my current working directory, alongside pyigrf12.cp36-win_amd64.pyd.

Now when I try import pyigrf12, I receive ImportError: DLL load failed: %1 is not a valid Win32 application.. Some stackoverflow posts seem to indicate that this is a problem with a conflict between a 32-bit dll and 64-bit Python. Can anyone offer insight into this? After more searching, I decided to try to use the anaconda version of mingw and libpython.

Method 3:

I run conda install mingw libpython, and it installs mingw 4.7-1 and libpython 2.1-py36_0. I run f2py -c igrf12.pyf igrf12.f --compiler=mingw32, and it fails with the following error message:

Building msvcr library: "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libvcruntime140.a" (from C:\Users\Sholes\AppData\Local\Continuum\anaconda3\vcruntime140.dll) objdump.exe: C:\Users\Sholes\AppData\Local\Continuum\anaconda3\vcruntime140.dll: File format not recognized Traceback (most recent call last): File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py", line 28, in <module> main() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 648, in main run_compile() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 633, in run_compile setup(ext_modules=[ext]) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\core.py", line 169, in setup return old_setup(**new_attr) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build.py", line 47, in run old_build.run(self) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\command\build.py", line 135, in run self.run_command(cmd_name) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build_ext.py", line 117, in run force=self.force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\ccompiler.py", line 733, in new_compiler compiler = klass(None, dry_run, force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 107, in __init__ msvcr_success = build_msvcr_library() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 399, in build_msvcr_library generate_def(dll_file, def_file) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 302, in generate_def raise ValueError("Symbol table not found") ValueError: Symbol table not found

Now the issue seems to relate to building libvcruntime140.a from vcruntime140.dll. Once again the dll file format is not recognized by objdump.exe.

Method 4:

My final attempt was to run f2py -c igrf12.pyf igrf12.f --compiler=msvc with the anaconda version of mingw installed. For this case, gfortran failed with this error:

C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\gfortran.bat -Wall -g -Wall -g -shared ..\..\..\AppData\Local\Temp\tmpugo__0q9\Release\igrf12.o -Lc:\users\sholes\appdata\local\continuum\anaconda3\mingw\lib\gcc\x86_64-w64-mingw32\4.7.0 -LC:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs -LC:\Users\Sholes\AppData\Local\Continuum\anaconda3\PCbuild\amd64 -o C:\Users\Sholes\AppData\Local\Temp\tmpugo__0q9\Release\extra-dll\libigrf12.75XJA5DX6DTO7YIZ7X6ZHJYTRDCCYQYR.gfortran-win_amd64.dll -Wl,--allow-multiple-definition -Wl,--output-def,C:\Users\Sholes\AppData\Local\Temp\tmpugo__0q9\Release\libigrf12.75XJA5DX6DTO7YIZ7X6ZHJYTRDCCYQYR.gfortran-win_amd64.def -Wl,--export-all-symbols -Wl,--enable-auto-import -static -mlong-double-64 gfortran.exe: error: unrecognized command line option '-mlong-double-64'

At this point, I just want to know if it is possible to create fortran extensions with my set up and f2py. I don't have any background compiling C or fortran extensions on Windows, and based on all of the questions related to issues with Python 3.6 scipy and numpy installation on Windows, it seems like this is a common problem without a simple solution.

Any feedback or insight would be greatly appreciated.

最满意答案

终于搞定了这个。

简洁版本:

确保为64位Python使用64位编译器(对mingw-w64进行三重检查)。 不像Windows上的f2py newb听起来那么明显。

长版:

我卸载了我现有的MinGW副本(我怀疑它是32位版本),而是从sourceforge下载了一个特定的64位mingw-w64 7.2.0 x86_64-7.2.0-release-posix-seh-rt_v5-rev1.7z ,特别是x86_64-7.2.0-release-posix-seh-rt_v5-rev1.7z 。 这个stackoverflow问题很有用。

我将“mingw64”文件夹解压缩并复制到我的C:驱动器( C:\mingw64 )中。 我将C:\mingw64\bin到我的用户Path。

我用conda uninstall mingw卸载了anaconda版本的MinGW。 请注意,只有在您之前使用conda安装MinGW时才需conda 。

在运行f2py -c igrf12.pyf igrf12.f --compiler=mingw32 (与igrf12.pyf相同的目录中,请参阅Scipy文档以了解如何生成* .pyf文件)时,创建了pyigrf12.cp36-win_amd64.pyd ,没有任何错误。 我终于可以成功import pyigrf12并访问底层的Fortran子程序(例如igrf12syn)。

注意,我也可以成功运行f2py -c igrf12.pyf igrf12.f --compiler=msvc ,但是我必须手动复制并粘贴libigrf12....gfortran-win_amd64.dll (在.\UNKNOWN\.libs\生成) .\UNKNOWN\.libs\ )与pyigrf12.cp36-win_amd64.pyd同一目录pyigrf12.cp36-win_amd64.pyd以避免ImportError: DLL load failed: The specified module could not be found. 在我的问题的方法2中提到。

重新迭代: 确保将 C:\mingw64\bin 添加到您的路径中!

顺便说一句,在macOS Sierra和Ubuntu上, f2py对我来说是无痛的。 如果以上仍然不适合你,我建议尝试Linux,macOS或Linux子系统Linux。

Finally got this working.

Short version:

Make sure you use 64-bit compilers (triple check the build for mingw-w64) for 64-bit Python. Not as obvious as it sounds to an f2py newb on Windows.

Long version:

I uninstalled my existing copy of MinGW (I suspect it was a 32 bit version) and instead downloaded a specific 64-bit build of mingw-w64 7.2.0 from sourceforge, specifically x86_64-7.2.0-release-posix-seh-rt_v5-rev1.7z. This stackoverflow question was useful.

I unzipped and copied the "mingw64" folder into my C: drive (C:\mingw64). I added C:\mingw64\bin to my user Path.

I uninstalled the anaconda version of MinGW with conda uninstall mingw. Note, this is only necessary if you've previously installed MinGW using conda.

Upon running f2py -c igrf12.pyf igrf12.f --compiler=mingw32 (in same directory as igrf12.pyf, see Scipy Documentation for how to generate *.pyf file), pyigrf12.cp36-win_amd64.pyd is created without any errors. I can finally import pyigrf12 successfully and access the underlying Fortran subroutines (e.g. igrf12syn).

Note, I can also run f2py -c igrf12.pyf igrf12.f --compiler=msvc successfully, but then I have to manually copy and paste the libigrf12....gfortran-win_amd64.dll (generated in .\UNKNOWN\.libs\) into the same directory as pyigrf12.cp36-win_amd64.pyd to avoid ImportError: DLL load failed: The specified module could not be found. mentioned in Method 2 of my question.

Just to re-iterate: Make sure C:\mingw64\bin is added to your path!

By the way, f2py was painless for me on macOS Sierra and Ubuntu. If the above still doesn't work for you, I recommend trying on Linux, macOS or Windows Subsystem for Linux.

在Windows 10上使用f2py和Python 3.6编译fortran模块(Compile fortran module with f2py and Python 3.6 on Windows 10)

我正在尝试(并且失败)在Windows 10上使用f2py和Python 3.6编译fortran模块(特别是来自BGS的igrf12.f)。使用Anaconda 4.4.10安装Python。

我的设置:

Python 3.6.3 | Anaconda自定义(64位)| (默认情况下,2017年10月15日,03:27:45)[winv上的MSC v.1900 64位(AMD64)] Windows 10企业版(1703版) NumPy 1.14.0

我遵循了SciPy文档中的 f2py指示以及Dr.Michael Hirsch的非常有用的指南 。 Dr.Hirsch创建了pyigrf12模块,但是通过pip安装失败了,这最初引起了我对f2py的兴趣。

我将概述我正在使用的一些方法。 无论方法如何,我总是首先使用f2py igrf12.f -m pyigrf12 -h igrf12.pyf创建一个* .pyf签名文件,并适当地添加intent(in / out)属性。

方法1:使用C:\ MinGW和--compiler=mingw32 方法2:使用C:\ MinGW和--compiler=msvc 方法3:使用anaconda版本的mingw和--compiler=mingw32 方法4:使用anaconda版本的mingw和--compiler=msvc

方法1:

对于后台,我在C:\ MinGW有MinGW,我已将C:\ MinGW \ bin添加到我的用户Path。 不幸的是,我没有安装这个版本的MinGW(我从同事那里继承了这台电脑),所以我不知道它的来源。 gcc --version和gfortran --version是5.3.0。

我运行f2py -c igrf12.pyf igrf12.f --compiler=mingw32 。 此失败并显示以下错误消息:

Building import library (arch=AMD64): "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libpython36.a" (from C:\Users\Sholes\AppData\Local\Continuum\anaconda3\python36.dll) objdump.exe: C:\Users\Sholes\AppData\Local\Continuum\anaconda3\python36.dll: File format not recognized Traceback (most recent call last): File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py", line 28, in <module> main() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 648, in main run_compile() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 633, in run_compile setup(ext_modules=[ext]) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\core.py", line 169, in setup return old_setup(**new_attr) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build.py", line 47, in run old_build.run(self) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\command\build.py", line 135, in run self.run_command(cmd_name) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build_ext.py", line 117, in run force=self.force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\ccompiler.py", line 733, in new_compiler compiler = klass(None, dry_run, force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 104, in __init__ build_import_library() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 416, in build_import_library return _build_import_library_amd64() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 472, in _build_import_library_amd64 generate_def(dll_file, def_file) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 302, in generate_def raise ValueError("Symbol table not found") ValueError: Symbol table not found

问题似乎涉及从python36.dll构建libpython36.a

快速谷歌搜索后, github论坛上pywafo的建议是使用msvc编译器而不是mingw32,导致方法2。

方法2:对于后台,与我的msvc相关的文件是从C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64\ 。 不确定这是否有帮助。

我运行f2py -c igrf12.pyf igrf12.f --compiler=msvc 。 这会产生两个文件:

pyigrf12.cp36-win_amd64.pyd在我当前的工作目录中, libigrf12.BMWM6WD5Y3O3UTOEQITBXCIICXVMBEZS.gfortran-win_amd64.dll位于.\UNKNOWN\.libs\

当我尝试import pyigrf12 ,首先我收到ImportError: DLL load failed: The specified module could not be found. 使用Dependency Walker,我收到很多错误:

但最明显的问题与libigrf12.BMWM6WD5Y3O3UTOEQITBXCIICXVMBEZS.gfortran-win_amd64.dll 。 我将此libigrf12 dll文件与pyigrf12.cp36-win_amd64.pyd一起移动到我当前的工作目录。

现在,当我尝试import pyigrf12 ,我收到ImportError: DLL load failed: %1 is not a valid Win32 application. 。 一些stackoverflow帖子似乎表明这是一个32位DLL和64位Python之间的冲突的问题。 有人能提供这方面的见解吗? 经过更多的搜索,我决定尝试使用anaconda版本的mingw和libpython。

方法3:

我运行conda install mingw libpython ,它安装了mingw 4.7-1和libpython 2.1-py36_0。 我运行f2py -c igrf12.pyf igrf12.f --compiler=mingw32 ,它失败并显示以下错误消息:

Building msvcr library: "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libvcruntime140.a" (from C:\Users\Sholes\AppData\Local\Continuum\anaconda3\vcruntime140.dll) objdump.exe: C:\Users\Sholes\AppData\Local\Continuum\anaconda3\vcruntime140.dll: File format not recognized Traceback (most recent call last): File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py", line 28, in <module> main() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 648, in main run_compile() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 633, in run_compile setup(ext_modules=[ext]) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\core.py", line 169, in setup return old_setup(**new_attr) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build.py", line 47, in run old_build.run(self) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\command\build.py", line 135, in run self.run_command(cmd_name) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build_ext.py", line 117, in run force=self.force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\ccompiler.py", line 733, in new_compiler compiler = klass(None, dry_run, force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 107, in __init__ msvcr_success = build_msvcr_library() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 399, in build_msvcr_library generate_def(dll_file, def_file) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 302, in generate_def raise ValueError("Symbol table not found") ValueError: Symbol table not found

现在问题似乎与从vcruntime140.dll构建libvcruntime140.a有关。 objdump.exe无法识别dll文件格式。

方法4:

我的最后一次尝试是运行安装了anaconda版本的mingw的f2py -c igrf12.pyf igrf12.f --compiler=msvc 。 对于这种情况,gfortran失败了这个错误:

C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\gfortran.bat -Wall -g -Wall -g -shared ..\..\..\AppData\Local\Temp\tmpugo__0q9\Release\igrf12.o -Lc:\users\sholes\appdata\local\continuum\anaconda3\mingw\lib\gcc\x86_64-w64-mingw32\4.7.0 -LC:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs -LC:\Users\Sholes\AppData\Local\Continuum\anaconda3\PCbuild\amd64 -o C:\Users\Sholes\AppData\Local\Temp\tmpugo__0q9\Release\extra-dll\libigrf12.75XJA5DX6DTO7YIZ7X6ZHJYTRDCCYQYR.gfortran-win_amd64.dll -Wl,--allow-multiple-definition -Wl,--output-def,C:\Users\Sholes\AppData\Local\Temp\tmpugo__0q9\Release\libigrf12.75XJA5DX6DTO7YIZ7X6ZHJYTRDCCYQYR.gfortran-win_amd64.def -Wl,--export-all-symbols -Wl,--enable-auto-import -static -mlong-double-64 gfortran.exe: error: unrecognized command line option '-mlong-double-64'

此时,我只想知道是否可以使用我的设置和f2py创建fortran扩展。 我没有在Windows上编译C或Fortran扩展的任何后台,并且基于与Windows上的Python 3.6 scipy和numpy安装问题相关的所有问题,似乎这是一个没有简单解决方案的常见问题。

任何反馈或见解将不胜感激。

I'm trying (and failing) to compile a fortran module (specifically igrf12.f from the BGS) using f2py and Python 3.6 on Windows 10. Python was installed using Anaconda 4.4.10.

My setup:

Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] on win32 Windows 10 Enterprise (version 1703) NumPy 1.14.0

I followed f2py directions from the SciPy documentation and a very helpful guide from Dr.Michael Hirsch. Dr.Hirsch has created the pyigrf12 module, but installation through pip failed for me, which is what initially sparked my interest in f2py.

I will outline a few of the methods I am using. Regardless of the method, I always begin by creating a *.pyf signature file using f2py igrf12.f -m pyigrf12 -h igrf12.pyf and adding intent(in/out) attributes appropriately.

Method 1: Use C:\MinGW and --compiler=mingw32 Method 2: Use C:\MinGW and --compiler=msvc Method 3: use anaconda version of mingw and --compiler=mingw32 Method 4: use anaconda version of mingw and --compiler=msvc

Method 1:

For background, I have MinGW at C:\MinGW and I have added C:\MinGW\bin to my user Path. Unfortunately, I did not install this version of MinGW (I inherited this computer from a colleague), so I do not know where it was sourced. The gcc --version and gfortran --version is 5.3.0.

I run f2py -c igrf12.pyf igrf12.f --compiler=mingw32. This fails with this error message:

Building import library (arch=AMD64): "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libpython36.a" (from C:\Users\Sholes\AppData\Local\Continuum\anaconda3\python36.dll) objdump.exe: C:\Users\Sholes\AppData\Local\Continuum\anaconda3\python36.dll: File format not recognized Traceback (most recent call last): File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py", line 28, in <module> main() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 648, in main run_compile() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 633, in run_compile setup(ext_modules=[ext]) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\core.py", line 169, in setup return old_setup(**new_attr) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build.py", line 47, in run old_build.run(self) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\command\build.py", line 135, in run self.run_command(cmd_name) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build_ext.py", line 117, in run force=self.force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\ccompiler.py", line 733, in new_compiler compiler = klass(None, dry_run, force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 104, in __init__ build_import_library() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 416, in build_import_library return _build_import_library_amd64() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 472, in _build_import_library_amd64 generate_def(dll_file, def_file) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 302, in generate_def raise ValueError("Symbol table not found") ValueError: Symbol table not found

The issue seems to involve building libpython36.a from python36.dll.

After a quick google search, a suggestion on a github forum for pywafo was to use the msvc compiler instead of mingw32, leading to Method 2.

Method 2: For background, the files related to my msvc are being pulled from C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64\. Not sure if that's helpful.

I run f2py -c igrf12.pyf igrf12.f --compiler=msvc. This produces two files:

pyigrf12.cp36-win_amd64.pyd in my current working directory and, libigrf12.BMWM6WD5Y3O3UTOEQITBXCIICXVMBEZS.gfortran-win_amd64.dll in .\UNKNOWN\.libs\

When I try to import pyigrf12, first I receive ImportError: DLL load failed: The specified module could not be found. Using Dependency Walker, I receive a lot of errors:

but the most obvious one to address is related to the libigrf12.BMWM6WD5Y3O3UTOEQITBXCIICXVMBEZS.gfortran-win_amd64.dll. I move this libigrf12 dll file to my current working directory, alongside pyigrf12.cp36-win_amd64.pyd.

Now when I try import pyigrf12, I receive ImportError: DLL load failed: %1 is not a valid Win32 application.. Some stackoverflow posts seem to indicate that this is a problem with a conflict between a 32-bit dll and 64-bit Python. Can anyone offer insight into this? After more searching, I decided to try to use the anaconda version of mingw and libpython.

Method 3:

I run conda install mingw libpython, and it installs mingw 4.7-1 and libpython 2.1-py36_0. I run f2py -c igrf12.pyf igrf12.f --compiler=mingw32, and it fails with the following error message:

Building msvcr library: "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libvcruntime140.a" (from C:\Users\Sholes\AppData\Local\Continuum\anaconda3\vcruntime140.dll) objdump.exe: C:\Users\Sholes\AppData\Local\Continuum\anaconda3\vcruntime140.dll: File format not recognized Traceback (most recent call last): File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py", line 28, in <module> main() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 648, in main run_compile() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py", line 633, in run_compile setup(ext_modules=[ext]) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\core.py", line 169, in setup return old_setup(**new_attr) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build.py", line 47, in run old_build.run(self) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\command\build.py", line 135, in run self.run_command(cmd_name) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build_ext.py", line 117, in run force=self.force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\ccompiler.py", line 733, in new_compiler compiler = klass(None, dry_run, force) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 107, in __init__ msvcr_success = build_msvcr_library() File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 399, in build_msvcr_library generate_def(dll_file, def_file) File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 302, in generate_def raise ValueError("Symbol table not found") ValueError: Symbol table not found

Now the issue seems to relate to building libvcruntime140.a from vcruntime140.dll. Once again the dll file format is not recognized by objdump.exe.

Method 4:

My final attempt was to run f2py -c igrf12.pyf igrf12.f --compiler=msvc with the anaconda version of mingw installed. For this case, gfortran failed with this error:

C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\gfortran.bat -Wall -g -Wall -g -shared ..\..\..\AppData\Local\Temp\tmpugo__0q9\Release\igrf12.o -Lc:\users\sholes\appdata\local\continuum\anaconda3\mingw\lib\gcc\x86_64-w64-mingw32\4.7.0 -LC:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs -LC:\Users\Sholes\AppData\Local\Continuum\anaconda3\PCbuild\amd64 -o C:\Users\Sholes\AppData\Local\Temp\tmpugo__0q9\Release\extra-dll\libigrf12.75XJA5DX6DTO7YIZ7X6ZHJYTRDCCYQYR.gfortran-win_amd64.dll -Wl,--allow-multiple-definition -Wl,--output-def,C:\Users\Sholes\AppData\Local\Temp\tmpugo__0q9\Release\libigrf12.75XJA5DX6DTO7YIZ7X6ZHJYTRDCCYQYR.gfortran-win_amd64.def -Wl,--export-all-symbols -Wl,--enable-auto-import -static -mlong-double-64 gfortran.exe: error: unrecognized command line option '-mlong-double-64'

At this point, I just want to know if it is possible to create fortran extensions with my set up and f2py. I don't have any background compiling C or fortran extensions on Windows, and based on all of the questions related to issues with Python 3.6 scipy and numpy installation on Windows, it seems like this is a common problem without a simple solution.

Any feedback or insight would be greatly appreciated.

最满意答案

终于搞定了这个。

简洁版本:

确保为64位Python使用64位编译器(对mingw-w64进行三重检查)。 不像Windows上的f2py newb听起来那么明显。

长版:

我卸载了我现有的MinGW副本(我怀疑它是32位版本),而是从sourceforge下载了一个特定的64位mingw-w64 7.2.0 x86_64-7.2.0-release-posix-seh-rt_v5-rev1.7z ,特别是x86_64-7.2.0-release-posix-seh-rt_v5-rev1.7z 。 这个stackoverflow问题很有用。

我将“mingw64”文件夹解压缩并复制到我的C:驱动器( C:\mingw64 )中。 我将C:\mingw64\bin到我的用户Path。

我用conda uninstall mingw卸载了anaconda版本的MinGW。 请注意,只有在您之前使用conda安装MinGW时才需conda 。

在运行f2py -c igrf12.pyf igrf12.f --compiler=mingw32 (与igrf12.pyf相同的目录中,请参阅Scipy文档以了解如何生成* .pyf文件)时,创建了pyigrf12.cp36-win_amd64.pyd ,没有任何错误。 我终于可以成功import pyigrf12并访问底层的Fortran子程序(例如igrf12syn)。

注意,我也可以成功运行f2py -c igrf12.pyf igrf12.f --compiler=msvc ,但是我必须手动复制并粘贴libigrf12....gfortran-win_amd64.dll (在.\UNKNOWN\.libs\生成) .\UNKNOWN\.libs\ )与pyigrf12.cp36-win_amd64.pyd同一目录pyigrf12.cp36-win_amd64.pyd以避免ImportError: DLL load failed: The specified module could not be found. 在我的问题的方法2中提到。

重新迭代: 确保将 C:\mingw64\bin 添加到您的路径中!

顺便说一句,在macOS Sierra和Ubuntu上, f2py对我来说是无痛的。 如果以上仍然不适合你,我建议尝试Linux,macOS或Linux子系统Linux。

Finally got this working.

Short version:

Make sure you use 64-bit compilers (triple check the build for mingw-w64) for 64-bit Python. Not as obvious as it sounds to an f2py newb on Windows.

Long version:

I uninstalled my existing copy of MinGW (I suspect it was a 32 bit version) and instead downloaded a specific 64-bit build of mingw-w64 7.2.0 from sourceforge, specifically x86_64-7.2.0-release-posix-seh-rt_v5-rev1.7z. This stackoverflow question was useful.

I unzipped and copied the "mingw64" folder into my C: drive (C:\mingw64). I added C:\mingw64\bin to my user Path.

I uninstalled the anaconda version of MinGW with conda uninstall mingw. Note, this is only necessary if you've previously installed MinGW using conda.

Upon running f2py -c igrf12.pyf igrf12.f --compiler=mingw32 (in same directory as igrf12.pyf, see Scipy Documentation for how to generate *.pyf file), pyigrf12.cp36-win_amd64.pyd is created without any errors. I can finally import pyigrf12 successfully and access the underlying Fortran subroutines (e.g. igrf12syn).

Note, I can also run f2py -c igrf12.pyf igrf12.f --compiler=msvc successfully, but then I have to manually copy and paste the libigrf12....gfortran-win_amd64.dll (generated in .\UNKNOWN\.libs\) into the same directory as pyigrf12.cp36-win_amd64.pyd to avoid ImportError: DLL load failed: The specified module could not be found. mentioned in Method 2 of my question.

Just to re-iterate: Make sure C:\mingw64\bin is added to your path!

By the way, f2py was painless for me on macOS Sierra and Ubuntu. If the above still doesn't work for you, I recommend trying on Linux, macOS or Windows Subsystem for Linux.