I have problems using f2py with python 3.12. To illustrate the problem I built two files where one contains fortran modules and the other the subroutine. It is important for me that the modules file is not the same with the subroutine.
The program (called promod.f90) that contains the modules is the following:
module pr
integer nm
end module
The program (called prova.f90) that contains the subroutine is the following:
subroutine pro(x,y)
!f2py intent(in) x
!f2py intent(out) y
use pr
implicit none
double precision x,y
nm=int(x)
y=sin(nm*1.d0)
end subroutine
The command for compilation is:
f2py3 -c -I$(pwd) -m prova prova.f90 promod.o -L$(pwd)
Cannot use distutils backend with Python>=3.12, using meson backend instead.
Using meson backend
Will pass –lower to f2py
See https://numpy.org/doc/stable/f2py/buildtools/meson.html
Reading fortran codes…
Reading file ‘prova.f90’ (format:free)
Post-processing…
Block: prova
Block: pro
In: :prova:prova.f90:pro
get_useparameters: no module pr info used by pro
Applying post-processing hooks…
character_backward_compatibility_hook
Post-processing (stage 2)…
Building modules…
Building module “prova”…
Generating possibly empty wrappers”
Maybe empty “prova-f2pywrappers.f”
Constructing wrapper function “pro”…
y = pro(x)
Wrote C/API module “prova” to file “./provamodule.c”
The Meson build system
Version: 1.5.1
Source dir: /tmp/tmpwk6dt64x
Build dir: /tmp/tmpwk6dt64x/bbdir
Build type: native build
Project name: prova
Project version: 0.1
Fortran compiler for the host machine: gfortran (gcc 13.3.0 “GNU Fortran (Debian 13.3.0-3) 13.3.0”)
Fortran linker for the host machine: gfortran ld.bfd 2.43
C compiler for the host machine: cc (gcc 13.3.0 “cc (Debian 13.3.0-3) 13.3.0”)
C linker for the host machine: cc ld.bfd 2.43
Host machine cpu family: x86_64
Host machine cpu: x86_64
Program /usr/bin/python3 found: YES (/usr/bin/python3)
Found pkg-config: YES (/usr/bin/pkg-config) 1.8.1
Run-time dependency python found: YES 3.12
Library quadmath found: YES
Build targets in project: 1
Found ninja-1.12.1 at /usr/bin/ninja
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /usr/bin/ninja -C /tmp/tmpwk6dt64x/bbdir
ninja: Entering directory `/tmp/tmpwk6dt64x/bbdir’
[6/6] Linking target prova.cpython-312-x86_64-linux-gnu.so
The object promod.o has been created by means of the command:
gfrotran -c -fPIC promod.f90
Then I start an interactive session:
python3
Python 3.12.4 (main, Jul 15 2024, 12:17:32) [GCC 13.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
import prova
Traceback (most recent call last):
File “”, line 1, in
ImportError: /home/stefano/Downloads/prova.cpython-312-x86_64-linux-gnu.so: undefined symbol: __pr_MOD_nm
Thank you
Stefano
Stefano Simonucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.