question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

numpy.f2py cannot build the module -- failed to import numpy

See original GitHub issue

I want to warp a simple fortran program for use with python. I am using the info on the f2py Getting Started page. However i fail in building my module. The only error I can find is a bit hidden in the .pyf file.

I have a simple fortran program (f90):

subroutine test(n, a)
! do nothing useful
    implicit none
    integer, intent(in)                   :: n
    integer, intent(out)                  :: a

    if (n .LT. 1) then
        a = 0
    else
        a = 10
    end if

end subroutine test

And I run the following line of code from the command line:

C:\...\dineof>"C:\Python38\python.exe" -m numpy.f2py test.f90 -m testModule -h test.pyf
Reading fortran codes...
        Reading file 'test.f90' (format:free)
Post-processing...
        Block: testModule
                        Block: test
Post-processing (stage 2)...
Saving signatures to file ".\test.pyf"

Resulting in test.pyf signature file:

!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module testModule ! in 
    interface  ! in :testModule
        subroutine test(n,a) ! in :testModule:test.f90
            integer intent(in) :: n
            integer intent(out) :: a
        end subroutine test
    end interface 
end python module testModule

! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/

Next, i run the following line of code, to build the module:

C:\...\dineof>"C:\Python38\python.exe" -m numpy.f2py -c test.pyf test.f90
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 "testModule" sources
creating C:\Users\..\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8
f2py options: []
f2py: test.pyf
Reading fortran codes...
        Reading file 'test.pyf' (format:free)
Post-processing...
        Block: testModule
                        Block: test
Post-processing (stage 2)...
Building modules...
        Building module "testModule"...
                Constructing wrapper function "test"...
                  a = test(n)
        Wrote C/API module "testModule" to file "C:\Users\..\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8\testModulemodule.c"
  adding 'C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8\fortranobject.c' to sources.
  adding 'C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8' to include_dirs.
copying C:\Python38\lib\site-packages\numpy\f2py\src\fortranobject.c -> C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8
copying C:\Python38\lib\site-packages\numpy\f2py\src\fortranobject.h -> C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8
build_src: building npy-pkg config files
running build_ext
No module named 'numpy.distutils._msvccompiler' in numpy.distutils; trying from distutils
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
Ingebouwde specs worden gebruikt.
COLLECT_GCC=C:\MinGW\bin\gfortran.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/8.2.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-8.2.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-8.2.0-3' --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --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-isl=/mingw --enable-libgomp --disable-libvtv --enable-nls --disable-build-format-warnings
Thread model: win32
gcc versie 8.2.0 (MinGW.org GCC-8.2.0-3)
customize Gnu95FCompiler
Ingebouwde specs worden gebruikt.
COLLECT_GCC=C:\MinGW\bin\gfortran.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/8.2.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-8.2.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-8.2.0-3' --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --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-isl=/mingw --enable-libgomp --disable-libvtv --enable-nls --disable-build-format-warnings
Thread model: win32
gcc versie 8.2.0 (MinGW.org GCC-8.2.0-3)
customize Gnu95FCompiler using build_ext
building 'testModule' extension
compiling C sources
creating C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users
creating C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...
creating C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...\AppData
creating C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...\AppData\Local
creating C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...\AppData\Local\Temp
creating C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...\AppData\Local\Temp\tmpupe2gsw9
creating C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8 -IC:\Python38\lib\site-packages\numpy\core\include -IC:\Python38\include -IC:\Python38\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\ATLMFC\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include -IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt /TcC:\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8\testModulemodule.c /FoC:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8\testModulemodule.obj
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8 -IC:\Python38\lib\site-packages\numpy\core\include -IC:\Python38\include -IC:\Python38\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\ATLMFC\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include -IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt /TcC:\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8\fortranobject.c /FoC:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8\fortranobject.obj
compiling Fortran sources
Fortran f77 compiler: C:\MinGW\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -O3 -funroll-loops
Fortran f90 compiler: C:\MinGW\bin\gfortran.exe -Wall -g -fno-second-underscore -O3 -funroll-loops
Fortran fix compiler: C:\MinGW\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -O3 -funroll-loops
compile options: '-IC:\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8 -IC:\Python38\lib\site-packages\numpy\core\include -IC:\Python38\include -IC:\Python38\include -c'
gfortran.exe:f90: test.f90
C:\MinGW\bin\gfortran.exe -Wall -g -Wall -g -shared ..\..\..\..\..\AppData\Local\Temp\tmpupe2gsw9\Release\test.o -Lc:\mingw\lib\gcc\mingw32\8.2.0 -LC:\Python38\libs -LC:\Python38\PCbuild\win32 -o C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\.libs\libtest.QWBASW5C3V6NKQGJCI5HJSKJXKLZZ6IL.gfortran-win32.dll -Wl,--allow-multiple-definition -Wl,--output-def,C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\libtest.QWBASW5C3V6NKQGJCI5HJSKJXKLZZ6IL.gfortran-win32.def -Wl,--export-all-symbols -Wl,--enable-auto-import -static -mlong-double-64
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\lib.exe /def:C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\libtest.QWBASW5C3V6NKQGJCI5HJSKJXKLZZ6IL.gfortran-win32.def /OUT:C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\libtest.QWBASW5C3V6NKQGJCI5HJSKJXKLZZ6IL.gfortran-win32.lib /MACHINE:X86
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:c:\mingw\lib\gcc\mingw32\8.2.0 /LIBPATH:C:\Python38\libs /LIBPATH:C:\Python38\PCbuild\win32 /LIBPATH:C:\Python38\libs /LIBPATH:C:\Python38\PCbuild\win32 /LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\ATLMFC\lib\x86 /LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\lib\x86 /LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x86 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\ucrt\x86 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\um\x86 /EXPORT:PyInit_testModule C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8\testModulemodule.obj C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8\fortranobject.obj C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\libtest.QWBASW5C3V6NKQGJCI5HJSKJXKLZZ6IL.gfortran-win32.lib /OUT:.\testModule.cp38-win32.pyd /IMPLIB:C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\Users\...\AppData\Local\Temp\tmpupe2gsw9\src.win32-3.8\testModule.cp38-win32.lib
copying C:\Users\...\AppData\Local\Temp\tmpupe2gsw9\Release\.libs\libtest.QWBASW5C3V6NKQGJCI5HJSKJXKLZZ6IL.gfortran-win32.dll -> .\testModule\.libs
Removing build directory C:\Users\...\AppData\Local\Temp\tmpupe2gsw9

Seemingly without any errors. However the module is not built. The only output I have is:

  • testModule.cp38-win32.pyd
  • testModlue/.libs/libtest.QWBASW5C3V6NKQGJCI5HJSKJXKLZZ6IL.gfortran-win32.dll

Error message:

The former containing the following error code:

Functions:
  a = test(n)
.    $Revision: $    can't initialize module testModule (failed to import numpy) numpy.core.multiarray failed to import  i   testModule.test() 1st argument (n) can't be converted to int    O:testModule.test   real    FATAL: module compiled as unknown endian    FATAL: module compiled as little endian, but detected different endianness at runtime   module compiled against API version 0x%x but this version of numpy is 0x%x  _ARRAY_API is NULL pointer  module compiled against ABI version 0x%x but this version of numpy is 0x%x  _ARRAY_API is not PyCapsule object  _ARRAY_API not found    _ARRAY_API  numpy.core._multiarray_umath     ]
  %d  ], arr.dims=[   %d unexpected array size: size=%d, arr_size=%d, rank=%d, effrank=%d, arr.nd=%d, dims=[ too many axes: %d (effrank=%d), expected rank=%d
   %d-th dimension must be fixed to %d but got %d (real index=%d)
 unexpected array size: new_size=%d, got array with arr_size=%d
 %d-th dimension must be fixed to %d but got %d
 unexpected array size: new_size=%d, got array with arr_size=%d (maybe too many free indices)
   %d-th dimension must be %d but got 0 (not defined).
    %d-th dimension must be fixed to %d but got %d
 <fortran object>    <fortran %U>    __name__    this fortran object is not callable no function to call delete non-existing fortran attribute   over-writing fortran routine    _cpointer   __doc__ __dict__    fortranobject.c: fortran_doc: len(p)=%zd>%zd=size: too long docstring required, increase size
  scalar  '%c'-   %s - no docs available  ,%d array(%d      

Numpy/Python version information:

I have installed the latest python (Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32) and numpy (1.18.3) on a Win 10 system. I also have MinGW fortran compiler installed. All directly under C.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:30 (24 by maintainers)

github_iconTop GitHub Comments

1reaction
charriscommented, Jun 16, 2022

Hmm, I don’t think I have any memberships that you don’t. Maybe I inherited something from owners.

0reactions
charriscommented, Jun 16, 2022

Do you have privileges for any of the other teams?

Read more comments on GitHub >

github_iconTop Results From Across the Web

f2py does not properly import, despite successfully compiling
Anther option to is to make it a static build, with the -static flag. This builds everything needed into a single file: python...
Read more >
Using F2PY — NumPy v1.25.dev0 Manual
To scan Fortran sources and generate a signature file, use. f2py -h ... sourcecode = sourcefile.read() f2py.compile(sourcecode, modulename='add') import add.
Read more >
F2py — Python-Fortran notebooks - Pierre Navaro
Write some fortran subroutines and just run f2py to create Python modules. ... plt import scipy.fftpack as sf import scipy.linalg as sl import...
Read more >
[Numpy-discussion] f2py compiled module not found by ...
module, built using f2py. ... installing without error, python cannot seem to see it (see log below). ... Can you import flib module...
Read more >
F2PY compilation study
Normal distutils cannot compile fortran sources. numpy.distutils uses f2py to ... Because distutils requires f2py to generate C/API module so that it can ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found