f2py fails because of Attribute-Error in python

88 Views Asked by At

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.

1

There are 1 best solutions below

6
cup On

If you try building using

f2py -c f2p.f95 -m isprim

or

python -m numpy.f2py -c f2p.f95 -m isprim

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

import isprim
print(isprim.isprima(5))
print(isprim.isprima(6))

The result is

1
0

Edit

If running on windows, f2py will generate two files. If the module name specified (-m) is fred

  1. A pyd file (fred.[python version].pyd)
  2. A dll (fred.libs\libf2p[some hex string and text].dll

Copy the DLL and the pyd file to the same directory as the python test program. In my case, after building, I had

U:\fortran\f2py>dir
 Volume in drive U has no label.
 Volume Serial Number is 78EE-1692

 Directory of U:\fortran\f2py

09/03/2024  06:59    <DIR>          .
09/03/2024  06:32    <DIR>          ..
09/03/2024  06:32               375 f2p.f90
09/03/2024  06:59    <DIR>          isprim
09/03/2024  06:59            27,136 isprim.cp39-win_amd64.pyd
09/03/2024  06:34                67 primtest.py
               3 File(s)         27,578 bytes
               3 Dir(s)  48,000,462,848 bytes free

U:\fortran\f2py>python primtest.py
Traceback (most recent call last):
  File "U:\fortran\f2py\primtest.py", line 1, in <module>
    import isprim
ImportError: DLL load failed while importing isprim: The specified module could not be found.

U:\fortran\f2py>copy isprim\.libs\*.dll .
isprim\.libs\libf2p.2AL5PRFC2YGEOTFQS23JZ4KOAGPGXN3J.gfortran-win_amd64.dll
        1 file(s) copied.

U:\fortran\f2py>python primtest.py
1
0

If you are using visual studio, you can use dumpbin to find out what DLLs, the pyd is looking for. In my case

U:\fortran\f2py>dumpbin /dependents isprim.cp39-win_amd64.pyd
Microsoft (R) COFF/PE Dumper Version 14.29.30151.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file isprim.cp39-win_amd64.pyd

File Type: DLL

  Image has the following dependencies:

    libf2p.2AL5PRFC2YGEOTFQS23JZ4KOAGPGXN3J.gfortran-win_amd64.dll
    python39.dll
    KERNEL32.dll
    VCRUNTIME140.dll
    api-ms-win-crt-stdio-l1-1-0.dll
    api-ms-win-crt-runtime-l1-1-0.dll

  Summary

        2000 .data
        1000 .pdata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text