Can't import module created by f2py "ImportError: DLL load failed"
See original GitHub issueI’m trying to use f2py to use some fortran code. I am trying to reproduce the example in the documentation of f2py
System: Windows 10 Python: Python 3.7.7 [MSC v.1916 64 bit (AMD64)] numpy: 1.18.1 I installed mingw64 and also the Visual C++ Redistributable after seeing some answers in SO suggesting that. However nothing has worked.
I have the file fib1.f:
SUBROUTINE FIB(A,N)
INTEGER N
REAL*8 A(N)
DO I=1,N
IF (I.EQ.1) THEN
A(I) = 0.0D0
ELSEIF (I.EQ.2) THEN
A(I) = 1.0D0
ELSE
A(I) = A(I-1) + A(I-2)
ENDIF
ENDDO
END
In the same folder I run python -m numpy.f2py -c fib1.f -m fib1 and that runs fine. It outputs:
py.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\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7\fib1module.c
creating C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.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\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7\fib1module.c"
adding 'C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7\fortranobject.c' to sources.
adding 'C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7' to include_dirs.
copying C:\ProgramData\Anaconda3\lib\site-packages\numpy\f2py\src\fortranobject.c -> C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7
copying C:\ProgramData\Anaconda3\lib\site-packages\numpy\f2py\src\fortranobject.h -> C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7
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
Found executable C:\Program Files\Git\usr\bin\DF.exe
Could not locate executable C:\Program
customize IntelItaniumVisualFCompiler
Could not locate executable efl
customize Gnu95FCompiler
Found executable C:\mingw64\bin\gfortran.exe
Using built-in specs.
COLLECT_GCC=C:\mingw64\bin\gfortran.exe
COLLECT_LTO_WRAPPER=C:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
customize Gnu95FCompiler
Using built-in specs.
COLLECT_GCC=C:\mingw64\bin\gfortran.exe
COLLECT_LTO_WRAPPER=C:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
customize Gnu95FCompiler using build_ext
building 'fib1' extension
compiling C sources
creating C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users
creating C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie
creating C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie\AppData
creating C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie\AppData\Local
creating C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie\AppData\Local\Temp
creating C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie\AppData\Local\Temp\tmpwkhj3p_i
creating C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7 -IC:\ProgramData\Anaconda3\lib\site-packages\numpy\core\include -IC:\ProgramData\Anaconda3\include -IC:\ProgramData\Anaconda3\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt /TcC:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7\fib1module.c /FoC:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7\fib1module.obj
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7 -IC:\ProgramData\Anaconda3\lib\site-packages\numpy\core\include -IC:\ProgramData\Anaconda3\include -IC:\ProgramData\Anaconda3\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt /TcC:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7\fortranobject.c /FoC:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7\fortranobject.obj
compiling Fortran sources
Fortran f77 compiler: C:\mingw64\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -O3 -funroll-loops
Fortran f90 compiler: C:\mingw64\bin\gfortran.exe -Wall -g -fno-second-underscore -O3 -funroll-loops
Fortran fix compiler: C:\mingw64\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -O3 -funroll-loops
compile options: '-IC:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7 -IC:\ProgramData\Anaconda3\lib\site-packages\numpy\core\include -IC:\ProgramData\Anaconda3\include -IC:\ProgramData\Anaconda3\include -c'
gfortran.exe:f77: fib1.f
C:\mingw64\bin\gfortran.exe -Wall -g -Wall -g -shared ..\..\..\..\..\..\..\..\..\..\..\AppData\Local\Temp\tmpwkhj3p_i\Release\fib1.o -LC:\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0 -LC:\ProgramData\Anaconda3\libs -LC:\ProgramData\Anaconda3\PCbuild\amd64 -o C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\.libs\libfib1.Y4DC6PBD2IBSWL6XGQRLVAUGMBCJD5RJ.gfortran-win_amd64.dll -Wl,--allow-multiple-definition -Wl,--output-def,C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\libfib1.Y4DC6PBD2IBSWL6XGQRLVAUGMBCJD5RJ.gfortran-win_amd64.def -Wl,--export-all-symbols -Wl,--enable-auto-import -static -mlong-double-64
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\bin\HostX86\x64\lib.exe /def:C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\libfib1.Y4DC6PBD2IBSWL6XGQRLVAUGMBCJD5RJ.gfortran-win_amd64.def /OUT:C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\libfib1.Y4DC6PBD2IBSWL6XGQRLVAUGMBCJD5RJ.gfortran-win_amd64.lib /MACHINE:X64
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0 /LIBPATH:C:\ProgramData\Anaconda3\libs /LIBPATH:C:\ProgramData\Anaconda3\PCbuild\amd64 /LIBPATH:C:\ProgramData\Anaconda3\libs /LIBPATH:C:\ProgramData\Anaconda3\PCbuild\amd64 /LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\lib\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64 /EXPORT:PyInit_fib1 C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7\fib1module.obj C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7\fortranobject.obj C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\libfib1.Y4DC6PBD2IBSWL6XGQRLVAUGMBCJD5RJ.gfortran-win_amd64.lib /OUT:.\fib1.cp37-win_amd64.pyd /IMPLIB:C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\src.win-amd64-3.7\fib1.cp37-win_amd64.lib
copying C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i\Release\.libs\libfib1.Y4DC6PBD2IBSWL6XGQRLVAUGMBCJD5RJ.gfortran-win_amd64.dll -> .\fib1\.libs
Removing build directory C:\Users\danie\AppData\Local\Temp\tmpwkhj3p_i
However, when I go into python and run import fib1, i get the following error:
Python 3.7.7 (default, Apr 15 2020, 05:09:04) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import fib1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: The specified module could not be found.
>>>
The same thing happens if I use an .f90 file or the 2nd method in documentation
Issue Analytics
- State:
- Created 3 years ago
- Comments:24 (12 by maintainers)
Top Results From Across the Web
Using f2py get ImportError: DLL load failed: The specified ...
For me the dll is created in the sub directory module_name\.libs\libmodule_namexxxxx.dll , when I copy it to my source directory, ...
Read more >DLL load failed: %1 is not a valid Win32 application" with gnu95
Issue 29 in f2py: "ImportError: DLL load failed: %1 is not a valid Win32 ... I am able to generate, but not to...
Read more >DLL load error of fortran module imported to python - Bytes
I have a f95 module (created with f2py) which I wish to import to a python code. ... ImportError: DLL load failed: invalid...
Read more >How to Fix Python ImportError: Dll Load Failed Error
This post offers 2 ways to fix the error "ImportError: Dll Load Failed: The specified module could not be found."
Read more >Common issues and error messages — Compiling on ... - DTU
unable to find vcvarsall.bat or cannot find -lmsvcr140. This might be caused by ... ImportError: DLL load failed: The specified module could not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi,
I came across the same issue when I tried to build PyTMatrix on Windows.
The main package is pytmatrix and it includes an f2py-based extension module named pytmatrix.fortran_tm.pytmatrix. By building the package, the following files are created together with many other files:
The libampld.lp.XN4PNFVBPLZONUAMUJEXLEI7ZJ4CLFGY.gfortran-win_amd64.dll file is created by f2py from the Fortran files that the pytmatrix.fortran_tm.pytmatrix module is intended to wrap.
When pytmatrix.fortran_tm.pytmatrix is imported, the following error is thrown.
I digged into the cause of the error, and identified that the pytmatrix.cp39-win_amd64.pyd module failed to find libampld.lp.XN4PNFVBPLZONUAMUJEXLEI7ZJ4CLFGY.gfortran-win_amd64.dll.
I also learned that on Windows, DLLs need to be in the same directory with the executable file (otherwise some other system directories are searched), and that Windows does not have a linker option equivalent to -rpath available in Linux/Unix. So in this case, libampld.lp.XN4PNFVBPLZONUAMUJEXLEI7ZJ4CLFGY.gfortran-win_amd64.dll needs to be in the same directory with pytmatrix.cp39-win_amd64.pyd.
I could not find distutils/f2py options to meet this requirement, so I revised the numpy.distutils code to achieve the required DLL file location (see attachment). After the code changes, the bdist_wininst command arranges the files as follows.
And I was finally able to import the pytmatrix.fortran_tm.pytmatrix module without an error and run the Fortran code in it correctly.
Obviously the revised distutils code has not been tested much, but I hope it provides a starting point for a better solution.
build_ext_rev2.patch.txt 27-Sep-2021 Revised the patch.
I wasn’t aware of the newer f2py docs, thanks @HaoZeke. I will take a look.