F2PY doesn't find a module - python

I am having difficulty with getting a f2py compiled module work in Python.
I have a piece of software written in Fortran that compiles well on a Linux 64bit machine.
Further on F2Py compiles a Python module that uses Fortran bits as well.
Here is how the Python module is compiled:
f2py --fcompiler=gfortran -I"path-to-dir-with-mod-files" -c -m mod_landems mod_landem.f90
But once I want to import that module I get an error (in Ipython):
----> 1 import mod_landems
ImportError: ./mod_landems.so: undefined symbol: __nesdis_landem_module_MOD_nesdis_landem
To be honest I am confused with this error. Search did not help much so I need to ask you here: how can I possibly make it work? If I put the python module code in the same directory as where the mod files are it produces same error message.
Here is a piece of my (primitive) code:
module n_landem
implicit none
! INPUT VARIABLES
real(8) Angle
real(8) Sm_Content
real(8) Veg_Frac
real(8) Soil_Temp
real(8) Land_Temp
real(8) Snow_Depth
real(8) Frequency
! OUTPUT VARIABLES
real(8) Emis_H
real(8) Emis_V
contains
subroutine landem
USE NESDIS_LANDEM_MODULE
USE TYPE_KINDS, ONLY : fp
call NESDIS_LandEM(Angle,Frequency,Sm_Content,Veg_Frac,Soil_Temp,Land_Temp,Snow_Depth,Emis_H,Emis_V)
end subroutine landem
end module n_landem
If I recall correctly some time ago this module was importable, but can't seem to make it work on either debian64 installation or debian32bit computer.

in your f2py call, you have to pass the libraries you are linking explicitly with '-l', the same you would pass it to your Fortran compiler (i.e. gfortran). Therefore, does
f2py -c --fcompiler=gfortran -I"path-to-dir-with-mod-files" --fcompiler=gfortran -I"path-to-dir-with-mod-files" -lNESDIS_LandEM_Module -m mod_landems mod_landem.f90 -m mod_landems mod_landem.f90
work for you ?
Best,
Max.

I'm working on a similar project and too am new to Fortran. Using the reference below I found that you import the fortran module just as you would import a library and then call a function similarly.
http://cens.ioc.ee/projects/f2py2e/usersguide/#the-quick-and-smart-way

Related

Can a statically linked python interpretter load python modules that contain native dynamic libraries?

I've statically linked a python interpretter (libpython.a) into a C++ program.
When I try to use it to import a python module that contains a native dynamic library component (a .so), I get an error that the core python library symbols are unavailable.
For example, if I try to:
import subprocess
I get:
ImportError: _posixsubprocess.so: undefined symbol: PyTuple_Type
Likewise, if I try:
import ctypes
I get:
ImportError: _ctypes.so: undefined symbol: PyFloat_Type
These two functions PyTuple_Type and PyFloat_Type are part of the core python library (libpython.a), so should be statically linked into the program.
Yet, when these dynamic libraries (_posixsubprocess.so or _ctypes.so) are loaded, they can't seem to find them.
Any ideas?
In general, when you load a dynamic library can its needed undefined external symbols be resolved by symbols that are statically defined/linked in the main program? I'm not sure I understand how that works (or if it's supposed to work).
The relevant parts of the test program are:
#include <Python.h>
int main() {
Py_Initialize();
PyRun_SimpleString("import subprocess")
}
I've figured out part of it:
By default the symbols in a program are not exported and made available to dynamic libraries that are loaded.
In order to export the symbols you can pass the --export-dynamic option to the linker. Also you may need --whole-archive so that the linker doesn't link out unused functions:
$ gcc -Wl,--export-dynamic myprogram.cc -Wl,--whole-archive libpython.a -Wl,--no-whole-archive
See: https://sourceware.org/binutils/docs-2.34/ld/Options.html#Options
Doing so fixes the problem, and the python modules now work, as the dynamic libraries can find the python API symbols from the main program.
I'm not sure if it's possible to just export the symbols from one particular library.
Also I'm not sure yet if Windows and Mac will have similar problems, or the equivalent solution on those platforms.

Problem with linking static and dynamic Fortran library with f2py

I am using the library written by M. Wimmer to compute pfaffians.
I have big code in python, in which I invoke the function from the package pfaffian.
However, I need to compute these pfaffians many times, therefore I would like to increase efficiency (using profiler I checked that computation of pfaffian consumes much time).
Therefore, I would like to use f2py tool to invoke functions from Fortran library in python code. The problem is, that I need only function skpfa which depends on other functions which belong to the library.
I tried to use instructions from here to link with static library libpfapack.a, however while importing the output module (which I called pfaf.so) I got the error message:
./pfaf.so: undefined symbol: skpfa_
I read that the problem can lie in the fact that I want to link static library, therefore I created dynamical library from provided sources using command:
gfortran -O3 -fimplicit-none -c -fPIC file.f -o file.o
for all source files, and then
gfortran -shared $(OBJECTS) -o libpfapack
I created following signature file pfaf.pyf:
python MODULE pfaffian
PUBLIC
INTERFACE
SUBROUTINE SKPFA(A, PFSFF, UPLO, MTHD, INFO)
double precision, intent(in) :: A(:,:)
double precision, intent(inout) :: p
character, intent(in), optional :: UPLO, MTHD
integer, intent(out), optional :: info
end subroutine skpfa
end interface
end python module pfaffian
and I invoked:
f2py -c --lower --fcompiler=gnu95 pfaf.pyf -L{path to directory with libpfapack} -lpfapack
and I got a message
/usr/bin/ld: cannot find -lpfapack
If I invoke f2py command without -lpfapack the things compile and produce file pfaf.so, however I get an error in python :
./pfaf.so: undefined symbol: skpfa_.
Does anyone have an idea how to fix it?

Python ImportError using F2py

I have two F90 files that I want to use with python, so I'm using f2py to compile them (together) and result a python module, I'm doing it like this:
f2py -c ControlParameters.F90 vector.F90 -m test
When I do this, everything is fine, and I can use functions and subroutines from those files with python.
But now, I need to use f2py with those two files, adding a couple of libraries, like -liomp5 or -lzmumps, I get the python module like in the upper case, but when I try to import it from python I have the following error:
ImportError: /opt/intel/composer_xe_2013.1.117/mkl/lib/intel64/libmkl_blacs_intelmpi_lp64.so: undefined symbol: MPI_Finalize
There are some libraries that cannot be used with f2py?

Importing fortran files to python

This is a very newbie question, but after searching in Google for a while, I haven't been able to find a solution. I'm writing a Python code using Eclipse (in Linux Mint) which needs some routines written in Fortran 77 (the code is in a file named fortran_code.f). I've used f2py to get the file fortran_code.so. This file is in the same folder as the Python code (../workspace/python_project/src). The Python code only includes:
import fortran_code
a = 10
fortran_code.fortran_subroutine(a)
Once an again, the result is:
Traceback (most recent call last): File
"/home/user/Documents/workspace/python_project/src/Main.py", line 7,
in <module>
import fortran_code ImportError: /home/user/Documents/workspace/python_project/src/fortran_code.so:
undefined symbol: PyCObject_Type
The Fortran subroutine code is:
SUBROUTINE fortran_subroutine(a)
REAL*8, intent(in) :: a
REAL*8 b
b=a*2
end subroutine fortran_subroutine
To get the .so file (fortran_code.so) I use:
f2py -c fortran_code.f -m fortran_code
The version of f2py is 2. And the version of Python is 2.7.4
Any help to solve this problem would be very useful.
Thanks!
In order to make it work I had to modify the Python Interpreter from Python 3.2 to Python 2.7 (since f2py produces code for Python 2.7). After editing a new Python module everything worked fine!!!
Thanks for all suggestions and comments, which were really helpful.

Issues when using f2py module in python code

I have a FORTRAN code that required the following compile command
gfortran -c interp.f -ffixed-format -ffix-line-length-none
I compiled the same using f2py module in python
from numpy import f2py
f2py.compile(open('interp.f').read(),modulename='interp',extra_flags='-ffixed-format -ffix-line-length-none',verbose=0)
It is unable to compile the module. It gives an error saying invalid file format '' at '-ffized-format'
Please help
Neither -ffixed-format, nor -ffix-line-length-none seem to be valid gfortran options. Consider using -ffixed-form and -ffixed-line-length-none instead.

Categories