title: Compile fortran module with f2py and Python 3.6 on Windows 10
body: <p>I'm trying (and failing) to compile a fortran module (specifically <a href="https://www.ngdc.noaa.gov/IAGA/vmod/igrf12.f" rel="noreferrer">igrf12.f</a> from the BGS) using f2py and Python 3.6 on Windows 10. Python was installed using Anaconda 4.4.10.</p>
  <p><strong>My setup:</strong></p>
  <ul>
  <li>Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017,
  03:27:45) [MSC v.1900 64 bit (AMD64)] on win32</li>
  <li>Windows 10 Enterprise (version 1703)</li>
  <li>NumPy 1.14.0</li>
  </ul>
  <p>I followed f2py directions from the <a href="https://docs.scipy.org/doc/numpy-dev/f2py/getting-started.html#the-smart-way" rel="noreferrer">SciPy documentation</a> and a very <a href="https://www.scivision.co/f2py-running-fortran-code-in-python-on-windows/" rel="noreferrer">helpful guide</a> 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.</p>
  <p>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 <code>f2py igrf12.f -m pyigrf12 -h igrf12.pyf</code> and adding intent(in/out) attributes appropriately.</p>
  <ul>
  <li>Method 1: Use C:\MinGW and <code>--compiler=mingw32</code></li>
  <li>Method 2: Use C:\MinGW and <code>--compiler=msvc</code></li>
  <li>Method 3: use anaconda version of mingw and <code>--compiler=mingw32</code></li>
  <li>Method 4: use anaconda version of mingw and <code>--compiler=msvc</code></li>
  </ul>
  <p><strong>Method 1:</strong></p>
  <p>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.</p>
  <p>I run <code>f2py -c igrf12.pyf igrf12.f --compiler=mingw32</code>. This fails with this error message:</p>
  <pre><code>Building import library (arch=AMD64):
  &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libpython36.a&quot; 
  (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 &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py&quot;, line 28, in &lt;module&gt;
      main()
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py&quot;, line 648, in main
      run_compile()
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py&quot;, line 633, in run_compile
      setup(ext_modules=[ext])
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\core.py&quot;, line 169, in setup
      return old_setup(**new_attr)
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\core.py&quot;, line 148, in setup
      dist.run_commands()
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py&quot;, line 955, in run_commands
      self.run_command(cmd)
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py&quot;, line 974, in run_command
      cmd_obj.run()
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build.py&quot;, line 47, in run
      old_build.run(self)
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\command\build.py&quot;, line 135, in run
      self.run_command(cmd_name)
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\cmd.py&quot;, line 313, in run_command
      self.distribution.run_command(command)
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py&quot;, line 974, in run_command
      cmd_obj.run()
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build_ext.py&quot;, line 117, in run
      force=self.force)
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\ccompiler.py&quot;, line 733, in new_compiler
      compiler = klass(None, dry_run, force)
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py&quot;, line 104, in __init__
      build_import_library()
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py&quot;, line 416, in build_import_library
      return _build_import_library_amd64()
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py&quot;, line 472, in _build_import_library_amd64
      generate_def(dll_file, def_file)
    File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py&quot;, line 302, in generate_def
      raise ValueError(&quot;Symbol table not found&quot;)
  ValueError: Symbol table not found
  </code></pre>
  <p><strong>The issue seems to involve building libpython36.a from python36.dll</strong>.</p>
  <p>After a quick google search, a suggestion on a <a href="https://github.com/wafo-project/pywafo/issues/26" rel="noreferrer">github forum for pywafo</a> was to use the msvc compiler instead of mingw32, leading to Method 2.</p>
  <p><strong>Method 2:</strong>
  For background, the files related to my msvc are being pulled from <code>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64\</code>. Not sure if that's helpful.</p>
  <p>I run <code>f2py -c igrf12.pyf igrf12.f --compiler=msvc</code>. This produces two files:</p>
  <ul>
  <li><code>pyigrf12.cp36-win_amd64.pyd</code> in my current working directory and,</li>
  <li><code>libigrf12.BMWM6WD5Y3O3UTOEQITBXCIICXVMBEZS.gfortran-win_amd64.dll</code> in <code>.\UNKNOWN\.libs\</code></li>
  </ul>
  <p>When I try to <code>import pyigrf12</code>, first I receive <code>ImportError: DLL load failed: The specified module could not be found.</code> Using Dependency Walker, I receive a lot of errors:</p>
  <p><a href="https://i.stack.imgur.com/ArUwz.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ArUwz.png" alt="f2py Dependency Walker Output" /></a></p>
  <p>but the most obvious one to address is related to the <code>libigrf12.BMWM6WD5Y3O3UTOEQITBXCIICXVMBEZS.gfortran-win_amd64.dll</code>. I move this libigrf12 dll file to my current working directory, alongside <code>pyigrf12.cp36-win_amd64.pyd</code>.</p>
  <p>Now when I try <code>import pyigrf12</code>, I receive <code>ImportError: DLL load failed: %1 is not a valid Win32 application.</code>. Some stackoverflow posts seem to indicate that this is a problem with a conflict between a 32-bit dll and 64-bit Python. <strong>Can anyone offer insight into this?</strong> After more searching, I decided to try to use the anaconda version of mingw and libpython.</p>
  <p><strong>Method 3:</strong></p>
  <p>I run <code>conda install mingw libpython</code>, and it installs mingw 4.7-1 and libpython 2.1-py36_0. I run <code>f2py -c igrf12.pyf igrf12.f --compiler=mingw32</code>, and it fails with the following error message:</p>
  <pre><code>Building msvcr library: 
  &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libvcruntime140.a&quot; 
  (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 &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py&quot;, line 28, in &lt;module&gt;
          main()
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py&quot;, line 648, in main
          run_compile()
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\f2py\f2py2e.py&quot;, line 633, in run_compile
          setup(ext_modules=[ext])
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\core.py&quot;, line 169, in setup
          return old_setup(**new_attr)
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\core.py&quot;, line 148, in setup
          dist.run_commands()
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py&quot;, line 955, in run_commands
          self.run_command(cmd)
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py&quot;, line 974, in run_command
          cmd_obj.run()
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build.py&quot;, line 47, in run
          old_build.run(self)
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\command\build.py&quot;, line 135, in run
          self.run_command(cmd_name)
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\cmd.py&quot;, line 313, in run_command
          self.distribution.run_command(command)
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\distutils\dist.py&quot;, line 974, in run_command
          cmd_obj.run()
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\command\build_ext.py&quot;, line 117, in run
          force=self.force)
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\ccompiler.py&quot;, line 733, in new_compiler
          compiler = klass(None, dry_run, force)
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py&quot;, line 107, in __init__
          msvcr_success = build_msvcr_library()
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py&quot;, line 399, in build_msvcr_library
          generate_def(dll_file, def_file)
        File &quot;C:\Users\Sholes\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\distutils\mingw32ccompiler.py&quot;, line 302, in generate_def
          raise ValueError(&quot;Symbol table not found&quot;)
      ValueError: Symbol table not found
  </code></pre>
  <p><strong>Now the issue seems to relate to building libvcruntime140.a from vcruntime140.dll.</strong> Once again the dll file format is not recognized by objdump.exe.</p>
  <p><strong>Method 4:</strong></p>
  <p>My final attempt was to run <code>f2py -c igrf12.pyf igrf12.f --compiler=msvc</code> with the anaconda version of mingw installed. For this case, gfortran failed with this error:</p>
  <pre><code>   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'
  </code></pre>
  <p>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.</p>
  <p>Any feedback or insight would be greatly appreciated.</p>