首页 > 解决方案 > ImportError:math03.pyc 中有错误的幻数或错误:无法找到 vcvarsall.bat

问题描述

我有一些简单的 Fortran .f90 代码。我想将其编译为 DLL。一切都很成功。我将 .DLL 更改为 .PYC 。通过python导入pyc,我得到了错误。那我该怎么办?(我在互联网上搜索了很多,但所有这些都是关于c或c ++而不是Fortran。我安装了Python 2和3。并且有​​intel Fortran编译器和Visual安装了 Studio 2019 和 MinGW。

数学03.f90:

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 < n)
    ind = 0
    do while (ind < 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

英特尔 fortran 编译器:

更新:

我使用这个命令

ifort /dll math03.f90

所以结果:

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

将 .dll 更改为 .pyc

Python:

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.
>>> import math03
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: Bad magic number in math03.pyc

我使用它的另一种方式:

f2py -c math03.f90 -m primemod

我收到了这个错误:

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:> 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 -> c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7
copying c:\python27amd64\lib\site-packages\numpy\f2py\src\fortranobject.h -> 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

如前所述收到此错误:

error: Unable to find vcvarsall.bat

更新:

另外我必须说https://symbols.hotell.kau.se/2016/12/15/python-fortran/ 对我不起作用。

更新#2

我做了一些更改,重新安装 Visual Studio 2019 到 2017,重新安装 Intel Parallel Studio 2019ifort并添加 vcvarsall.bat到 PATH,卸载MINGW gfortran编译器,还稍微编辑了我的 Fortran 文件和 CMD 命令。

python -m numpy.f2py -c fib1.f -m fib1

所以我得到这个错误:

error: extension 'fib1' has Fortran sources but no Fortran compiler found

详情如下:

C:\Users\admin\Documents\GitHub\scikits.scattpy\src\math03\FIB>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:> 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 -> c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.h -> 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

更新#3:作为Python错误中的所有条件:文件...球形.py...导入f_utils...ImportError:没有名为f_utils的模块 ,特别是在它中UPDATE #1f2py成功运行。

标签: pythonvisual-studiofortranintel-fortran

解决方案


推荐阅读