[Python] Problem with passing variables between Python and Fortran using F2py

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I have been attempting to call a simple Fortran subroutine from python using f2py without success. The Fortran subroutine just adds two numbers defined in the python code. The Fortran code is:

    C
    SUBROUTINE TESTLIB(A,B,C)
    C
    REAL, intent (in) :: A, B
    REAL, intent (out) :: C
    C = A + B
    END SUBROUTINE TESTLIB


    Python:

    import numpy as np
    import testlib2

    A = 3
    B = 2
    C = testlib2.testlib(A, B)
    print("C = ", C)



    I created testlib2.pyd file from the command line with

    python -m numpy.f2py -c -m testlib2 testlib2.f


    Then placed testlib2.pyd file in the same directory as the python file (testlib.py). I had to install meson, ninja, cmake, charset-normalizer and pkgconfig libraries to make F2py work with Python 3.12.

    When I run the python code testlib.py (Spyder 5.5) I receive the following error:

    File c:\users\bcn\documents\hps\hps fortran\testlib.py:6
    C = testlib2.testlib(A, B)

    TypeError: testlib2.testlib() missing required argument 'c' (pos 3).


    The call to the Fortran subroutine appears to requiring the C variable to be defined but has been defined as an "out" variable. Not sure why it is doing this. The examples I have seen indicate that the Fortran subroutine needs to have the out variable listed. Using Windows 11 and Python 3.12. Any help would be appreciated. Thanks.

    Continue reading...

Compartilhe esta Página