Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't import module created by f2py "ImportError: DLL load failed" #16416

Closed
danvip10 opened this issue May 28, 2020 · 26 comments
Closed

Can't import module created by f2py "ImportError: DLL load failed" #16416

danvip10 opened this issue May 28, 2020 · 26 comments

Comments

@danvip10
Copy link

I'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

@mattip
Copy link
Member

mattip commented May 31, 2020

I see traces of x86_64-w64-mingw32 (32-bit gcc) even though you are using a 64-bit Python: MSC v.1916 64 bit (AMD64). Maybe somehow you have set your PATH to include 32-bit components from mingw? This stackoverflow suggests you can use dumpbin on the fib1.PYD file (which is a renamed DLL) to determine if it is 64 or 32 bits. Another potential cause for this message is if a needed support DLL is not found, however Anaconda is usually quite good about this as long as you have activated your Anaconda environment.

@danvip10
Copy link
Author

danvip10 commented Jun 1, 2020

Hi @mattip, although I wasn't able to use dumpbin as suggested in the stackoverflow answer, I followed the current chosen answer (opening the file in the text editor) and determined the PYD file is 64 bits. I also checked again and I can't find any path that points to the 32-bit version.
Is there anyway to verify the other potential cause you mentioned?

@mattip
Copy link
Member

mattip commented Jun 1, 2020

You can try using depends.exe to see what other dlls fib1.pyd depends on. Trivially, it might be libgfortran*.dll, which may not be on your PATH.

@danvip10
Copy link
Author

danvip10 commented Jun 2, 2020

I have been going through those threads and looking at my installation. It seems that indeed i don't have libgfortran*.dll. However, I tried several installations of mingw and could not find such a file anywhere. Any idea where I could get that?

@mattip
Copy link
Member

mattip commented Jun 2, 2020

I use choco install mingw which puts it in c:\programdata\cholatey\lib\mingw\tools\install\mingw64\bin. That directory is added to the path by the installer, so it should JustWork™

@mattip
Copy link
Member

mattip commented Jun 2, 2020

For me it is named libgfortran-4.dll

@danvip10
Copy link
Author

danvip10 commented Jun 2, 2020

I just installed using chocolatey and the same thing continues to happen.. I tried looking for any libgfortran-*.dll files in the installation folder but there are none. The closest thing there is is two files named libgfortran.a and libgfortran.spec. Any clues?

@danvip10
Copy link
Author

danvip10 commented Jun 2, 2020

Also, using the depends.exe you mentioned, and opening the generate pyd file, i get the following error:

Error: At least one required implicit or forwarded dependency was not found.
Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: A circular dependency was detected.
Warning: At least one delay-load dependency module was not found.
Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.

@mattip
Copy link
Member

mattip commented Jun 3, 2020

i get the following error:

That is normal, even for well-behaved DLLs.

I just installed using chocolatey ...

Dunno, I just followed the recipe and it worked. After you install chocolatey (with admin permissions), you may need to open a new DOS window (as a regular user) so the PATH changes percolate through. Maybe a chocolatey forum might be able to help more.

@danvip10
Copy link
Author

danvip10 commented Jun 4, 2020

I see. I installed with admin permissions and opened a new DOS windows afterwards, but still the libgfortran dll wasn't there. Could you tell in which folder yours was located?
I am trying to download just that dll separately, but it seems more dll's might be missing..

@mattip
Copy link
Member

mattip commented Jun 4, 2020

I reported that above

@rossbar
Copy link
Contributor

rossbar commented Jul 11, 2020

@danvip10 is this issue still active? It sounds like the gremlin likely lies in system configuration rather than something that is NumPy-specific. It may not be much help, but buried in the azure-steps-windows.yml is a procedure for configuring a windows system for the CI.

@danvip10
Copy link
Author

Hi @rossbar, the issue is indeed still active. I will give it a try with what you suggested.

@oussamazt
Copy link

Hello @danvip10
were you able to solve this problem ?
i have exactly the same issue on win10 ( I tried on linux it worked smoothly )
i'm able to compile the .f90 program, but unable to import it
File "<string>", line 1, in <module> ImportError: DLL load failed while importing PPP: Le module spécifié est introuvable.
I'm using a Python 64 with MinGw 64 as well.
note that this issue doesn't appear if I dont use write read

@zlamere
Copy link

zlamere commented Nov 23, 2020

Hey oussamazt this command was able to work for me
python -m numpy.f2py -c --fcompiler=gnu95 --compiler=mingw32 -m HELLO hello.f90

Don't ask my why it worked

@danvip10
Copy link
Author

Hi @oussamazt

I haven't been able to solve the problem because I've been using MacOs lately and haven't installed this yet. Let me know if the approach proposed by @zlamere works for you though.

@pearu pearu added this to To do in f2py core Feb 19, 2021
@pearu pearu added this to To do in f2py usage Feb 19, 2021
@pearu pearu removed this from To do in f2py core Feb 19, 2021
@Siglab-code
Copy link

I had the exact problem recently and the solution is to download the required Microsoft Visual C++ 14.0. This page well explains what you need to do: https://www.scivision.dev/python-windows-visual-c-14-required/

@HaoZeke
Copy link
Member

HaoZeke commented Aug 24, 2021

This seems to be an issue of poor documentation, and not a true bug. Should be closed with a PR to improve the docs.

@HaoZeke HaoZeke self-assigned this Aug 24, 2021
@kjym3
Copy link

kjym3 commented Sep 26, 2021

Hi,

I came across the same issue when I tried to build PyTMatrix on Windows.

  • System: Windows 10
  • Python: Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
  • numpy: 1.21.2
  • Compilers: MinGW-w64 8.1.0 (x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z), VS 2019

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:

pytmatrix\__init__.py
pytmatrix\.libs\libampld.lp.XN4PNFVBPLZONUAMUJEXLEI7ZJ4CLFGY.gfortran-win_amd64.dll
pytmatrix\fortran_tm\pytmatrix.cp39-win_amd64.pyd
pytmatrix\fortran_tm\__init__.py

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.

ImportError: DLL load failed while importing pytmatrix: The specified module could not be found.

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.

pytmatrix\__init__.py
pytmatrix\fortran_tm\pytmatrix.cp39-win_amd64.pyd
pytmatrix\fortran_tm\libampld.lp.XN4PNFVBPLZONUAMUJEXLEI7ZJ4CLFGY.gfortran-win_amd64.dll
pytmatrix\fortran_tm\__init__.py

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.

@AlanMThomason
Copy link

Thanks so much! This resolved a big stumbling block for me. I put the *.dll file together with the *.pyd as you suggested and can now import. Thank you for your detailed post.

@carlkl
Copy link
Member

carlkl commented Jun 15, 2022

Works if you try out the following configuration:

Any recent CPython 64-bit version

UCRT64 Msys2 GCC toolchain, i.e.:

gcc version 12.1.0 (Rev2, Built by MSYS2 project)

and the following command line:

python -m numpy.f2py --fcompiler=gnu95 --compiler=mingw32 -c fib1.f -m fib1

The flag --compiler=mingw32 is essential to this command.

Without this flag one gets a gfortran compiled and linked fib1.cp39-win_amd64.pyd which depends on an MSVC compiled and linked fib1.JM4RYCDWRKL5C4DV7NFX5DGITH46ROUZ.gfortran-win_amd64.dll

@carlkl
Copy link
Member

carlkl commented Jun 15, 2022

I propose to close this issue.

@HaoZeke HaoZeke removed their assignment Jun 15, 2022
@HaoZeke
Copy link
Member

HaoZeke commented Jun 15, 2022

I propose to close this issue.

I think a documentation PR should close this as mentioned earlier.

This seems to be an issue of poor documentation, and not a true bug. Should be closed with a PR to improve the docs.

Please feel free to take it up if you'd like. The right location to make changes can be seen in the Windows F2PY documentation PR (rendered here)

@carlkl
Copy link
Member

carlkl commented Jun 15, 2022

I wasn't aware of the newer f2py docs, thanks @HaoZeke. I will take a look.

@Owen-Dechow
Copy link

I had the exact same problem. The solution I found was to do everything from MSYS2 CLANG64. I don't personally like this solution but, it works for now.

@HaoZeke
Copy link
Member

HaoZeke commented Nov 20, 2023

Closing since the new documentation has quite a bit on Windows builds (mostly now we just suggest offloading to the SciPy configuration with rtools)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests