1

I am trying to wrap some Fortran code using f2py, but running into the following problem. Let's say that I have a minimum working example in Fortran as,

MODULE Circle
!---------------------------------------------------------------------
!
!  Module containing definitions of variables needed to
!  compute the area of a circle of radius r
!
!---------------------------------------------------------------------
   REAL, PARAMETER :: Pi = 3.1415927
   REAL :: radius
END MODULE Circle

I save this file as circlemodule.F90 and run,

python -m numpy.f2py -c circlemodule.F90 -m circlemodule

Then f2py runs without errors and I obtain a file circlemodule.cp37-win_amd64.pyd. If I now run the simple script

import circlemodule

print(circlemodule.__doc__)

in the same directory I get the expected output and everything seems fine. However here is my issue, if I take the above example and add an extra line:

MODULE Circle
!---------------------------------------------------------------------
!
!  Module containing definitions of variables needed to
!  compute the area of a circle of radius r
!
!---------------------------------------------------------------------
   REAL, ALLOCATABLE, DIMENSION(:,:) :: B
   REAL, PARAMETER :: Pi = 3.1415927
   REAL :: radius
END MODULE Circle

The code that I actually want to wrap contains a lot of allocatable definitions like this one, and if I am reading this correctly f2py should be able to handle these according to the example at the bottom of this webpage: https://numpy.org/doc/stable/f2py/python-usage.html However when I try to wrap this code using the exact same steps as before, and run the python script to import, I get the following,

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

Does anyone have any idea why this happens? Somehow the module f2py creates is now no longer recognized by python. I am using python 3.7.9 and my fortran compiler is ifort 2021. Thanks!

EDIT: It might be interesting to note that in the output of f2py the following line is present

getarrdims:warning: assumed shape array, using 0 instead of ':'

Perhaps this is a clue to the problem?

Jasper
  • 87
  • 7
  • So what is the actual code that does not work? Is it the second snapshot? Did you confirm it also causes the error? What is the exact Python script that loads it and errors out? – Vladimir F Героям слава Dec 08 '20 at 21:33
  • Yes the second snapshot, if I save that as circlemodule.F90, run ```python -m numpy.f2py -c circlemodule.F90 -m circlemodule``` and then in that directory run ```import circlemodule``` I get the error. – Jasper Dec 08 '20 at 21:36
  • Allocatable arrays in modules should be possible https://stackoverflow.com/questions/52597454/using-allocatable-arrays-from-modules-in-f2py – Vladimir F Героям слава Dec 08 '20 at 21:41
  • I have added some extra info in the edit. – Jasper Dec 08 '20 at 21:45
  • FWIW, I cannot reproduce your error, everything works as expected with the commands given, admittedly I'm using different versions (Python 3.6.5, Numpy 1.16.1, ifort 19.0.1.144). The import error seems to suggest that either the DLL was not created (so you should see an error in F2PY's output, not just a warning) or there is mismatch between the F2PY/Python combination that created the DLL and that is trying to load the extension (although the manner in which you create the F2PY extension, suggests this is not the case). – jbdv Dec 09 '20 at 08:59
  • Hi thanks for your help. I have tried reverting to python 3.6.6 but still I get the same error. Maybe I can find that version of ifort somewhere, thanks for the suggestion. – Jasper Dec 09 '20 at 09:27

0 Answers0