Compile fortran module with f2py and Python 3.6 on Windows 10

前端 未结 3 1561

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.1

相关标签:
3条回答
  • 2020-12-31 15:26

    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.

    0 讨论(0)
  • 2020-12-31 15:37

    I had the same issue. The top answer helps a lot. I would like to add some comments about the missing DLLs. A useful tool to help you find out which DLLs are missing is Process Monitor. You only need to add a filter python.exe and track which DLLs are failed to load. For me, the following ones are missing:

    • libgcc_s_seh-1.dll
    • libgfortran-5.dll
    • libquadmath-0.dll
    • libwinpthread-1.dll

    I just need to copy them from the bin/ directory of my MinGW toolchain to the same directory of python.exe

    0 讨论(0)
  • 2020-12-31 15:40

    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.

    0 讨论(0)
提交回复
热议问题