Compile fortran module with f2py and Python 3.6 on Windows 10

痞子三分冷 提交于 2019-11-30 22:29:10

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.

I had the same issue, in particular the: ValueError: Symbol table not found of Method 3.

dsholes' answer was the solution, plus 2 minor points:

  • uninstall all versions of minggw
  • download mingw64 (in my case: x86_64-8.1.0-posix-seh-rt_v6-rev0 ), install it
  • Add mingw64/bin to PATH
  • Then the compilation with f2py -c fortran_file.F90 -m module_name --compiler=mingw32 runs fine, and can be imported properly in Python: import module_name

However:

  1. your modules should be compiled in the same windows partition as Mingw32 is installed. Else (mingw was under C:\, my project under D:\, I get the following error, due to Relative paths not being read from one partition to another: f2py target file 'C:\\...' not generated

  2. the compiled file may still require some external librairies. In my case, the libquadmath-0.dll in mingw64\bin is needed and should remain in the PATH at all time. Else I get a the following error: ImportError: DLL load failed:. I ended up identifying these dll and copying them within my project.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!