title: ImportError: Bad magic number in math03.pyc or error: Unable to find vcvarsall.bat
body: <p>I have some simple Fortran .f90 codes. I want to compile it as DLL. everything is successful. I changed .DLL to .PYC .  import pyc by python ,I got error.so what should I do?(I searched a lot in internet but all of them is about c or c++ not Fortran. I have both Python 2 and 3 installed. And having intel Fortran compiler and Visual Studio 2019 installed and MinGW installed.</p>
  
  <p>math03.f90:</p>
  
  <pre><code>program example
  implicit none
  integer (kind=8), parameter :: n = 5000
  integer (kind=8), dimension(0:n-1) :: primes
  
  call get_primes(n,primes)
  end program
  
  subroutine get_primes(n,primes)
    implicit none
    integer (kind=8) :: n
    integer (kind=8), dimension(0:n-1) :: primes
    integer (kind=8) :: primes_found, prime_cand, ind
  
    !primes(0) = 2
    !primes_found = 1
    !prime_cand = 3
  
    do while (primes_found &lt; n)
      ind = 0
      do while (ind &lt; primes_found)
        if (mod(prime_cand,primes(ind)) == 0) then
          exit
        else
          ind = ind + 1
        end if
      end do
      if (ind == primes_found) then
        primes(primes_found) = prime_cand
        primes_found = primes_found + 1
      end if
      prime_cand = prime_cand + 1
    end do
  end subroutine
  </code></pre>
  
  <p>intel fortran compiler:</p>
  
  <p>UPDATE:</p>
  
  <p>I  use this command</p>
  
  <pre><code>ifort /dll math03.f90
  </code></pre>
  
  <p>so the result :</p>
  
  <pre><code>Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 19.0.4.228 Build 20190417
  Copyright (C) 1985-2019 Intel Corporation.  All rights reserved.
  
  Microsoft (R) Incremental Linker Version 14.21.27702.2
  Copyright (C) Microsoft Corporation.  All rights reserved.
  
  -out:math03.dll
  -dll
  -implib:math03.lib
  math03.obj
  </code></pre>
  
  <p>changing .dll to .pyc</p>
  
  <p>python:</p>
  
  <pre><code>Python 2.7.16 (v2.7.16:413a49145e, Mar  4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import math03
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  ImportError: Bad magic number in math03.pyc
  </code></pre>
  
  <p>another way I use this:</p>
  
  <pre><code>f2py -c math03.f90 -m primemod
  </code></pre>
  
  <p>I got this error:</p>
  
  <pre><code>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 "primemod" sources
  f2py options: []
  f2py:&gt; c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7\primemodmodule.c
  creating c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7
  Reading fortran codes...
          Reading file 'math03.f90' (format:free)
  Post-processing...
          Block: primemod
                          Block: example
                          Block: get_primes
  Post-processing (stage 2)...
  Building modules...
          Building module "primemod"...
                  Constructing wrapper function "get_primes"...
                    get_primes(primes,[n])
          Wrote C/API module "primemod" to file "c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7\primemodmodule.c"
    adding 'c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7\fortranobject.c' to sources.
    adding 'c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7' to include_dirs.
  copying c:\python27amd64\lib\site-packages\numpy\f2py\src\fortranobject.c -&gt; c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7
  copying c:\python27amd64\lib\site-packages\numpy\f2py\src\fortranobject.h -&gt; c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7
  build_src: building npy-pkg config files
  running build_ext
  customize MSVCCompiler
  customize MSVCCompiler using build_ext
  get_default_fcompiler: matching types: '['gnu', 'intelv', 'absoft', 'compaqv', 'intelev', 'gnu95', 'g95', 'intelvem', 'intelem', 'flang']'
  customize GnuFCompiler
  Could not locate executable g77
  Could not locate executable f77
  customize IntelVisualFCompiler
  Could not locate executable ifort
  Could not locate executable ifl
  customize AbsoftFCompiler
  Could not locate executable f90
  customize CompaqVisualFCompiler
  Could not locate executable DF
  customize IntelItaniumVisualFCompiler
  Could not locate executable efl
  customize Gnu95FCompiler
  Found executable C:\MinGW\bin\gfortran.exe
  Using built-in specs.
  COLLECT_GCC=C:\MinGW\bin\gfortran.exe
  COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
  Target: mingw32
  Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls
  Thread model: win32
  gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
  customize Gnu95FCompiler
  Using built-in specs.
  COLLECT_GCC=C:\MinGW\bin\gfortran.exe
  COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
  Target: mingw32
  Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls
  Thread model: win32
  gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
  customize Gnu95FCompiler using build_ext
  building 'primemod' extension
  compiling C sources
  error: Unable to find vcvarsall.bat
  </code></pre>
  
  <p>got this error as mentioned:</p>
  
  <pre><code>error: Unable to find vcvarsall.bat
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Also I must say that <a href="https://symbols.hotell.kau.se/2016/12/15/python-fortran/" rel="nofollow noreferrer">https://symbols.hotell.kau.se/2016/12/15/python-fortran/</a>
  Does not work for me.</p>
  
  <p>UPDATE #2</p>
  
  <p>I make some changes , reinstall Visual Studio 2019 to 2017 ,reinstall Intel Parallel Studio 2019 for <code>ifort</code> and add  <code>vcvarsall.bat</code> to PATH , uninstall <code>MINGW gfortran</code> compiler ,Also edited my Fortran file and CMD command a little .</p>
  
  <pre><code>python -m numpy.f2py -c fib1.f -m fib1
  </code></pre>
  
  <p>so I get this error :</p>
  
  <pre><code>error: extension 'fib1' has Fortran sources but no Fortran compiler found
  </code></pre>
  
  <p>as details there is :</p>
  
  <pre><code>C:\Users\admin\Documents\GitHub\scikits.scattpy\src\math03\FIB&gt;python -m numpy.f2py -c fib1.f -m fib1
  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:&gt; c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7\fib1module.c
  creating c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7
  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\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7\fib1module.c"
    adding 'c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7\fortranobject.c' to sources.
    adding 'c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7' to include_dirs.
  copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.c -&gt; c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7
  copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.h -&gt; c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7
  build_src: building npy-pkg config files
  running build_ext
  customize MSVCCompiler
  customize MSVCCompiler using build_ext
  get_default_fcompiler: matching types: '['gnu', 'intelv', 'absoft', 'compaqv', 'intelev', 'gnu95', 'g95', 'intelvem', 'intelem', 'flang']'
  customize GnuFCompiler
  Could not locate executable g77
  Could not locate executable f77
  customize IntelVisualFCompiler
  Could not locate executable ifort
  Could not locate executable ifl
  customize AbsoftFCompiler
  Could not locate executable f90
  customize CompaqVisualFCompiler
  Could not locate executable DF
  customize IntelItaniumVisualFCompiler
  Could not locate executable efl
  customize Gnu95FCompiler
  Could not locate executable gfortran
  Could not locate executable f95
  customize G95FCompiler
  Could not locate executable g95
  customize IntelEM64VisualFCompiler
  customize IntelEM64TFCompiler
  Could not locate executable efort
  Could not locate executable efc
  customize PGroupFlangCompiler
  don't know how to compile Fortran code on platform 'nt'
  warning: build_ext: f77_compiler=None is not available.
  
  building 'fib1' extension
  error: extension 'fib1' has Fortran sources but no Fortran compiler found
  </code></pre>
  
  <p><strong>UPDATE #3: as all the conditions in <a href="https://stackoverflow.com/questions/57300786/python-error-file-spherical-py-import-f-utils-importerror-no-module">Python Error : File ... spherical.py... import f_utils...ImportError: No module named f_utils</a>  and specially in It's <code>UPDATE #1</code> ,<code>f2py</code> works successfully .</strong></p>