Call Fortran from Python on spyder, windows 10, "AttributeError: module 'myflib' has no attribute 'function'"

93 Views Asked by At

I'm currently trying to call a function contained in Fortran code from a Python script. To do this, I've tried the example available on https://www.matecdev.com/posts/fortran-in-python.html.

I've created "my_fortran_lib.f90" which contains the function defined as

!
! my_fortran_lib.f90:
!
function foo(a) result(b)
    implicit none
   
    real(kind=8), intent(in) :: a(:,:)
    complex(kind=8) :: b(size(a,1),size(a,2))
   
    b = exp((0,1)*a)
   
end function foo

Then I executed the following command f2py3 -c -m myflib2 my_fortran_lib.f90 on the command prompt (I use windows 10) I have 2 outputs "myflib2.cp311-win_amd64.pyd" and "libmy_fortr.VGRWKJX2MMMS3CK3OCJQHKECLLH3CKTE.gfortran-win_amd64.dll" contained in the folder "myflib2" (which is in the same folder as my others libraries)

However, when I run my code on spyder,

import myflib2
a = np.array([[1,2,3,4], [5,6,7,8]], order='F')
myflib2.foo(a)

I have the following results: results

myflib2.foo(a)
Traceback (most recent call last):

  Cell In[10], line 1
    myflib2.foo(a)

AttributeError: module 'myflib2' has no attribute 'foo' 

it can find the folder myflib2 but can't run the function that is supposed to be in the Fortran code.

would you have any idea of the source of my problem?

thanks in advance,

I have tried to use

  • other format of Fortran as f90, f
  • other function

but it does not work

0

There are 0 best solutions below