Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building 64-bit Python extensions with f2py on Windows

I'm attempting to build a Python extension from Fortran source using Numpy's f2py.py script. I'm following the steps from http://www.scipy.org/F2PY_Windows (web archive). My system is Windows 7 64-bit, and I primarily use Python 2.7.3 [MSC v.1500 64 bit (AMD64)]. I have Numpy-MKL 1.7.1, from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

Here is what I've tried:

  1. Choose a Fortran compiler. Since I'm using 64-bit Python, a 64-bit Fortran compiler is required. From MinGW-w64, I've tried a few of the personal builds.
  2. Open a new cmd.exe shell, and edit the path to where I extracted the GCC compilers, i.e.:
    • set PATH=%PATH%;c:\gnu\mingw64\bin
    • set C_INCLUDE_PATH=C:\gnu\mingw64\include
  3. Try the f2py.py build with this command:

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

The output is (removing noise lines at <--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

Note that the first time this is run, three new files are build in C:\Python27\libs:

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

Do I need to install anything else? Do I need to provide other options to f2py? More generally speaking, it possible to build an extension for MSVC-build Python from GNU compilers?

like image 765
Mike T Avatar asked Jun 05 '13 00:06

Mike T


People also ask

Is the pybind11 extension faster than the CPython extension?

Feel free to switch between those configurations for comparison, but remember to go back and update the properties that you set earlier for the release configuration. In the output, you might see that the PyBind11 extension isn't as fast as the CPython extension, though it should be faster than the pure Python implementation.

How do I build an extension DLL with Py_limited_API?

The /MDd option is used to build the Python debug binaries (such as python_d.exe ), but selecting it for an extension DLL still causes the build error with Py_LIMITED_API. Right-click the C++ project and select Build to test your configurations (both Debug and Release ).

How do I add a Python extension to Visual Studio Code?

Make sure the PYTHONHOME environment variable is set to the Python interpreter you want to use. The C++ projects in Visual Studio rely on this variable to locate files such as python.h, which are used when creating a Python extension. Right-click the solution in Solution Explorer and select Add > New Project.

What is the best way to distribute Python extensions?

The distutils approach works well for most extensions; documentation on using distutils to build and package extension modules is available in Distributing Python Modules (Legacy version). If you find you really need to do things manually, it may be instructive to study the project file for the winsound standard library module.


2 Answers

It seems that the solution is to just comment out the exception line. See this blog post.

like image 138
DaveP Avatar answered Sep 18 '22 13:09

DaveP


I had the similar error when trying to install ggplot with

pip install -U ggplot

The solution was found here: https://github.com/obspy/obspy/wiki/Installation-via-Anaconda

Anaconda for Windows ships with C and Fortran compilers, however compiling on a 64bit Windows may require a small patch by changing line no. 331 in AnacondaInstallDir\Lib\site-packages\numpy\distutils\fcompiler\gnu.py to pass #raise NotImplementedError("Only MS compiler supported with gfortran on win64").

like image 33
jaycode Avatar answered Sep 17 '22 13:09

jaycode