I am trying to use a Fortran 95 function in Python 3.10 with f2py. I compile as follows:
python -m numpy.f2py -c -m isprim f2p.f95
My F95-Code:
function isprima (p)
integer, intent(in) :: p
integer :: i
logical :: isprima
isprima = .true.
if (p/=2) then
testloop: do i=2,int(floor(sqrt(real(p))))
if (modulo(p, i) == 0) then
isprima = .false.
exit testloop
end if
end do testloop
end if
end function isprima
My Python-Code:
import numpy as np
import isprim
print(isprim.isprima(5))
The error-message is:
Exception has occurred: AttributeError module 'isprim' has no attribute 'isprima' File "C:\Users\josch\Documents\Programmierung\Fortran95\fortran2python.py", line 3, in print(isprim.isprima(5)) AttributeError: module 'isprim' has no attributeĀ 'isprima'
I don't know what is wrong.
When I tried to solve it by myself, I found solutions on the internet, for instance to name the F95-Function in lowercase. It didn't work, obviously. I couldn't find other solutions.
If you try building using
or
On Linux, it will generate a .so file. This will be picked up by the python program when it runs. Not strictly necessary to include numpy - it just makes the execution time a bit longer since numpy has to be loaded
The result is
Edit
If running on windows, f2py will generate two files. If the module name specified (-m) is fred
Copy the DLL and the pyd file to the same directory as the python test program. In my case, after building, I had
If you are using visual studio, you can use dumpbin to find out what DLLs, the pyd is looking for. In my case