2

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.

mikitk
  • 100
  • 1
  • 9
  • A little update, I had to return to this step and recompile the module. It took me two steps: - recompile object files with `-fPIC` option - compile python module with f2py: `f2py -c -m pyModule $ObjectFiles pyModule.f90` – mikitk Apr 27 '13 at 03:38

2 Answers2

1

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.

mjhoffmann
  • 374
  • 2
  • 3
0

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