尝试使用 f2py 包装时出错(最小工作示例)

Error while trying to wrap using f2py (Minimum working example)

我正在尝试 运行 列出的 Minimum working example 文件:
https://docs.scipy.org/doc/numpy-dev/f2py/getting-started.html

在 运行 在 Cygwin 中执行以下命令后

python c:/Python27/Scripts/f2py.py -c fib1.f -m fib1 --compiler=mingw32

命令退出并出现以下错误:

error: Command "gcc -O2 -Wall -Wstrict-prototypes -DNPY_MINGW_USE_CUSTOM_MSVCR -D__MSVCRT_VERSION__=0x0900 -Ic:\cygwin\tmp\tmprggtye\src.win32-2.7 -IC:\Python27\lib\site-packages\numpy\core\include -IC:\Python27\include -IC:\Python27\PC -c c:\cygwin\tmp\tmprggtye\src.win32-2.7\fib1module.c -o c:\cygwin\tmp\tmprggtye\Release\cygwin\tmp\tmprggtye\src.win32-2.7\fib1module.o" failed with exit status 1

下面是打印在控制台上的完整输出:

$ python c:/Python27/Scripts/f2py.py -c fib1.f -m fib1 --compiler=mingw32
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:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c
creating c:\cygwin\tmp\tmpucrmay\src.win32-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:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c"
  adding 'c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fortranobject.c' to sources.
  adding 'c:\cygwin\tmp\tmpucrmay\src.win32-2.7' to include_dirs.
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.c -> c:\cygwin\tmp\tmpucrmay\src.win32-2.7
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.h -> c:\cygwin\tmp\tmpucrmay\src.win32-2.7
build_src: building npy-pkg config files
running build_ext
Cannot build msvcr library: "msvcr90d.dll" not found
customize Mingw32CCompiler
customize Mingw32CCompiler using build_ext
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
Found executable C:\cygwin\bin\DF.exe
Found executable C:\cygwin\bin\DF.exe
customize IntelItaniumVisualFCompiler
Could not locate executable efl
customize Gnu95FCompiler
Found executable C:\cygwin\bin\gfortran.exe
Found executable C:\cygwin\bin\gfortran.exe
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'fib1' extension
compiling C sources
C compiler: gcc -O2 -Wall -Wstrict-prototypes

creating c:\cygwin\tmp\tmpucrmay\Release
creating c:\cygwin\tmp\tmpucrmay\Release\cygwin
creating c:\cygwin\tmp\tmpucrmay\Release\cygwin\tmp
creating c:\cygwin\tmp\tmpucrmay\Release\cygwin\tmp\tmpucrmay
creating c:\cygwin\tmp\tmpucrmay\Release\cygwin\tmp\tmpucrmay\src.win32-2.7
compile options: '-DNPY_MINGW_USE_CUSTOM_MSVCR -D__MSVCRT_VERSION__=0x0900 -Ic:\cygwin\tmp\tmpucrmay\src.win32-2.7 -IC:\Python27\lib\site-packages\numpy\core\include -IC:\Python27\include -IC:\Python27\PC -c'
gcc -O2 -Wall -Wstrict-prototypes -DNPY_MINGW_USE_CUSTOM_MSVCR -D__MSVCRT_VERSION__=0x0900 -Ic:\cygwin\tmp\tmpucrmay\src.win32-2.7 -IC:\Python27\lib\site-packages\numpy\core\include -IC:\Python27\include -IC:\Python27\PC -c c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c -o c:\cygwin\tmp\tmpucrmay\Release\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.o
Found executable C:\cygwin\bin\gcc.exe
In file included from C:\Python27\include/Python.h:86:0,
                 from c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:17:
C:\Python27\include/intobject.h:46:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PyInt_AsUnsignedLongLongMask’
 PyAPI_FUNC(unsigned PY_LONG_LONG) PyInt_AsUnsignedLongLongMask(PyObject *);
                                   ^
In file included from C:\Python27\include/Python.h:88:0,
                 from c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:17:
C:\Python27\include/longobject.h:50:1: warning: parameter names (without types) in function declaration
 PyAPI_FUNC(PyObject *) PyLong_FromLongLong(PY_LONG_LONG);
 ^
In file included from C:\Python27\include/Python.h:58:0,
                 from c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:17:
C:\Python27\include/pyconfig.h:314:23: error: unknown type name ‘__int64’
 # define PY_LONG_LONG __int64
                       ^
C:\Python27\include/pyport.h:793:34: note: in definition of macro ‘PyAPI_FUNC’
 #       define PyAPI_FUNC(RTYPE) RTYPE
                                  ^
C:\Python27\include/longobject.h:52:12: note: in expansion of macro ‘PY_LONG_LONG’
 PyAPI_FUNC(PY_LONG_LONG) PyLong_AsLongLong(PyObject *);
            ^
In file included from C:\Python27\include/Python.h:88:0,
                 from c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:17:
C:\Python27\include/longobject.h:53:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PyLong_AsUnsignedLongLong’
 PyAPI_FUNC(unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLong(PyObject *);
                                   ^
C:\Python27\include/longobject.h:54:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PyLong_AsUnsignedLongLongMask’
 PyAPI_FUNC(unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLongMask(PyObject *);
                                   ^
In file included from C:\Python27\include/Python.h:58:0,
                 from c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:17:
C:\Python27\include/pyconfig.h:314:23: error: unknown type name ‘__int64’
 # define PY_LONG_LONG __int64
                       ^
C:\Python27\include/pyport.h:793:34: note: in definition of macro ‘PyAPI_FUNC’
 #       define PyAPI_FUNC(RTYPE) RTYPE
                                  ^
C:\Python27\include/longobject.h:55:12: note: in expansion of macro ‘PY_LONG_LONG’
 PyAPI_FUNC(PY_LONG_LONG) PyLong_AsLongLongAndOverflow(PyObject *, int *);
            ^
In file included from C:\Python27\include/Python.h:8:0,
                 from c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:17:
C:\Python27\include/pyconfig.h:314:23: error: unknown type name ‘__int64’
 # define PY_LONG_LONG __int64
                       ^
C:\Python27\lib\site-packages\numpy\core\include/numpy/npy_common.h:263:9: note: in expansion of macro ‘PY_LONG_LONG’
 typedef PY_LONG_LONG npy_longlong;
         ^
In file included from C:\Python27\lib\site-packages\numpy\core\include/numpy/ndarraytypes.h:4:0,
                 from C:\Python27\lib\site-packages\numpy\core\include/numpy/ndarrayobject.h:18,
                 from C:\Python27\lib\site-packages\numpy\core\include/numpy/arrayobject.h:4,
                 from /tmp/tmpucrmay/src.win32-2.7/fortranobject.h:13,
                 from c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:19:
C:\Python27\lib\site-packages\numpy\core\include/numpy/npy_common.h:264:31: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘npy_ulonglong’
 typedef unsigned PY_LONG_LONG npy_ulonglong;
                               ^
In file included from C:\Python27\lib\site-packages\numpy\core\include/numpy/ndarraytypes.h:4:0,
                 from C:\Python27\lib\site-packages\numpy\core\include/numpy/ndarrayobject.h:18,
                 from C:\Python27\lib\site-packages\numpy\core\include/numpy/arrayobject.h:4,
                 from /tmp/tmpucrmay/src.win32-2.7/fortranobject.h:13,
                 from c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:19:
C:\Python27\lib\site-packages\numpy\core\include/numpy/npy_common.h:566:17: error: unknown type name ‘npy_ulonglong’
         typedef npy_ulonglong npy_uint64;
                 ^
In file included from C:\Python27\lib\site-packages\numpy\core\include/numpy/ndarraytypes.h:1781:0,
                 from C:\Python27\lib\site-packages\numpy\core\include/numpy/ndarrayobject.h:18,
                 from C:\Python27\lib\site-packages\numpy\core\include/numpy/arrayobject.h:4,
                 from /tmp/tmpucrmay/src.win32-2.7/fortranobject.h:13,
                 from c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:19:
C:\Python27\lib\site-packages\numpy\core\include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it by " \
  ^
c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c: In function ‘initfib1’:
c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:331:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
   Py_TYPE(&PyFortran_Type) = &PyType_Type;
   ^
c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c: At top level:
c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c:112:12: warning: ‘f2py_size’ defined but not used [-Wunused-function]
 static int f2py_size(PyArrayObject* var, ...)
            ^
error: Command "gcc -O2 -Wall -Wstrict-prototypes -DNPY_MINGW_USE_CUSTOM_MSVCR -D__MSVCRT_VERSION__=0x0900 -Ic:\cygwin\tmp\tmpucrmay\src.win32-2.7 -IC:\Python27\lib\site-packages\numpy\core\include -IC:\Python27\include -IC:\Python27\PC -c c:\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.c -o c:\cygwin\tmp\tmpucrmay\Release\cygwin\tmp\tmpucrmay\src.win32-2.7\fib1module.o" failed with exit status 1

我浏览了很多链接试图找到一些无济于事的解决方案。
如果有人能帮助我解决问题,将不胜感激。
我正在使用 Windows 7(64 位)和 Python 2.7.11.

为了子孙后代,把我做的贴出来
在尝试了评论中提到的内容以及更多内容(无法全部记住)之后,我尝试了以下操作:

python c:/Python27/Scripts/f2py.py -c fib1.f -m fib1 --compiler=msvc --fcompiler==g77

g77 是现成的 fortran 编译器(任何其他 fortran 编译器都应该可以)。

我遇到了以下错误:

$ python c:/Python27/Scripts/f2py.py -c fib1.f -m fib1 --compiler=msvc  --fcompiler=g77
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
  commands have different --compiler options: ['mingw32', 'msvc'], using first in list as default
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:\cygwin\tmp\tmpn3krmz\src.win32-2.7\fib1module.c
creating c:\cygwin\tmp\tmpn3krmz\src.win32-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:\cygwin\tmp\tmpn3krmz\src.win32-2.7\fib1module.c"
  adding 'c:\cygwin\tmp\tmpn3krmz\src.win32-2.7\fortranobject.c' to sources.
  adding 'c:\cygwin\tmp\tmpn3krmz\src.win32-2.7' to include_dirs.
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.c -> c:\cygwin\tmp\tmpn3krmz\src.win32-2.7
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.h -> c:\cygwin\tmp\tmpn3krmz\src.win32-2.7
build_src: building npy-pkg config files
running build_ext
customize MSVCCompiler
customize MSVCCompiler using build_ext
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
Found executable C:\cygwin\bin\DF.exe
Found executable C:\cygwin\bin\DF.exe
customize IntelItaniumVisualFCompiler
Could not locate executable efl
customize Gnu95FCompiler
Found executable C:\cygwin\bin\gfortran.exe
Found executable C:\cygwin\bin\gfortran.exe
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'fib1' extension
compiling C sources
error: Unable to find vcvarsall.bat

为了摆脱最后的错误,我从
中得到灵感 1) error: Unable to find vcvarsall.bat
2) How do I point easy_install to vcvarsall.bat?

我在第 245 行和第 249 行将 vcvarsall.bat 的位置硬编码到 C:/Python27/Lib/distutils/msvc9compilerfind_vcvarsall() 函数到变量 toolsdirproductdir 中。

我这样做是因为按照 2(上面)中提到的设置环境变量不起作用。

可能不是一个优雅的解决方案,但它奏效了。