在Windows上使用f2py构建64位Python扩展

12

我正在尝试使用Numpy的f2py.py脚本从Fortran源代码构建Python扩展。我按照http://www.scipy.org/F2PY_Windows (网络档案馆)中的步骤进行操作。我的系统是Windows 7 64位,我主要使用Python 2.7.3 [MSC v.1500 64 bit (AMD64)]。我安装了来自http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy的Numpy-MKL 1.7.1。

这是我尝试过的:

  1. 选择Fortran编译器。由于我使用的是64位Python,所以需要使用64位Fortran编译器。我已经尝试了一些个人构建版本,来自MinGW-w64
  2. 打开一个新的cmd.exe shell,并编辑路径到我提取GCC编译器的位置,即:
    • set PATH=%PATH%;c:\gnu\mingw64\bin
    • set C_INCLUDE_PATH=C:\gnu\mingw64\include
  3. 使用以下命令尝试f2py.py构建:

    C:\Python27\python.exe C:\Python27\Scripts\f2py.py -c --fcompiler=gnu95 --compiler=mingw32 -lmsvcr90 -m foo foo.f90

输出为(删除<--snip-->下的噪音行):

running build
<--snip-->
Reading fortran codes...
        Reading file 'foo.f90' (format:free)
Post-processing...
        Block: foo
                        Block: hello
Post-processing (stage 2)...
Building modules...
        Building module "foo"...
                Constructing wrapper function "hello"...
                  hello()
        Wrote C/API module "foo" to file "c:\users\mtoews\appdata\local\temp\tmpjr6qop\src.win-amd64-2.7\foomodule.c"
  adding 'c:\users\mtoews\appdata\local\temp\tmpjr6qop\src.win-amd64-2.7\fortranobject.c' to sources.
  adding 'c:\users\mtoews\appdata\local\temp\tmpjr6qop\src.win-amd64-2.7' to include_dirs.
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.c -> c:\users\mtoews\appdata\local\temp\tmpjr6qop\src.win-amd64-2.7
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.h -> c:\users\mtoews\appdata\local\temp\tmpjr6qop\src.win-amd64-2.7
build_src: building npy-pkg config files
running build_ext
Looking for python27.dll
Building import library (arch=AMD64): "C:\Python27\libs\libpython27.a" (from C:\Windows\system32\python27.dll)
Building msvcr library: "C:\Python27\libs\libmsvcr90.a" (from C:\Windows\winsxs\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b\msvcr90.dll)
Cannot build msvcr library: "msvcr90d.dll" not found
customize Mingw32CCompiler
customize Mingw32CCompiler using build_ext
customize Gnu95FCompiler
Found executable c:\gnu\mingw64\bin\gfortran.exe
Found executable c:\gnu\mingw64\bin\gfortran.exe
Traceback (most recent call last):
  File "C:\Python27\Scripts\f2py.py", line 24, in <module>
    main()
<--snip-->
  File "C:\Python27\lib\site-packages\numpy\distutils\fcompiler\gnu.py", line 331, in get_libraries
    raise NotImplementedError("Only MS compiler supported with gfortran on win64")
NotImplementedError: Only MS compiler supported with gfortran on win64

请注意第一次运行时会在C:\Python27\libs中生成三个新文件:

  • libmsvcr90.a
  • libpython27.a
  • python27.def

我需要安装其他东西吗?我需要为f2py提供其他选项吗?更普遍地说,是否可以使用GNU编译器构建MSVC-build Python的扩展程序?

3个回答

8
似乎解决方案就是注释掉异常行。请参考这篇博客文章

1
看起来能够工作。此外,我不得不从f2py中删除一个“-lmsvcr90”标志。 - Mike T
和我情况一样,只不过我没有直接运行f2py,而是通过numpy distutils内部调用它。这篇帖子帮了我很多(注意删除生成的libmsvcr90.a文件):http://mechaos.de/wordpress/?p=303#comment-169 - Caleb Hattingh

1

当我尝试使用以下命令安装ggplot时,遇到了类似的错误:

pip install -U ggplot

解决方案在这里找到了:https://github.com/obspy/obspy/wiki/Installation-via-Anaconda

Windows版Anaconda附带C和Fortran编译器,但是在64位Windows上进行编译可能需要通过更改AnacondaInstallDir\Lib\site-packages\numpy\distutils\fcompiler\gnu.py中的第331行来进行小修补,以传递#raise NotImplementedError("Only MS compiler supported with gfortran on win64")。


0

这个问题应该在未来的版本中得到修复,已经讨论更改

看起来这个更改没有被包含在1.9.x维护分支中,但是已经在主分支和之后的版本中实现了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接