2017년 8월 21일 월요일

How to import existing fortran code into python

Since there are many existing fortran codes, it would be better to use them
in python. One possible way to do it is to create a shared object(.so) file by using f2py. 
Simple example of using f2py can be found easily. 
But, in case of complicate fortran program which use 'make',  or in case of the subroutine we want to use is a part of larger library, it is not clear how to 
achieve it. 

The steps are 
(1) Prepare a module code which includes all the subroutines which wants to be 
    ported to python. 
    It is recommended the subroutines includes both input and outputs explicitly 
    by 'intent(in)' and 'intent(out)' properties. 

(2) create a static library for the subroutine (first compile for object files)     
     ar crsv lib[name].a [object files]

(3) use f2py to create signature file (.pyf) . 
    f2py [source file] -m [package name] -h [signature file name]

    Here the [source file] contains the subroutines or its wrapper to be imported to python. signature file (.pyf) contains a module for python which contains subroutines.
Importing sub module does not work well. Thus, always prepare [source file]
as a wrapper with subroutines not modules.

(4) edit the signature fil( .pyf) as necessary. (Only leave the subroutines to be imported) 

(5) created shared object library(.so) file using f2py 
    (If path of library is the same, "-L." would work?) 
    f2py -c [signature file .pyf] [source file] -L[absolute path for library]-l[library name] -llapack

(6) In the python,
    import [package name]

     and now one can use subroutines,
    [packagename].[subroutine]

(7) in case that the library(.so) file is located in different folder,
    add the library path before import

    import sys
    sys.path.insert(0,'[Library Path]')



  • The simple case: If there is only one fortran file, one can do 
  • (1) edit/comment the fortran file "my_lib.f90" with "!f2py " comments or explicit "intent" expressions.
  • (2)  Use following command to create  " my_lib.so " file which can be imported in python by "import my_lib" , 

f2py -c -m my_lib my_lib.f90

# Another way to use f2py is 
   directly add "cf2py intent([in/out]) [variable]" in the fortran source code 
   and compile 
   "python -m numpy.f2py -c -m [module name] [fortran source]" 

# In Windows, there seems to be some issue with the version of mingw-w64. 
  It seems I have to use "x86_64" version of mingw-w64 instead of "i686" version. 
  More details on the installation. 
  (https://python-at-risoe.pages.windenergy.dtu.dk/compiling-on-windows/configuration.html) 

# In Windows, one can use "ar" to create library as like LINUX with mingw-w64.

# Currently(2025.02.25), there seems to be a problem using f2py and "meson" build system in Windows... I am not sure how to fix the error. The same code can be compiled in linux.  









python library path 설정

Note that the python path is not the same as the system $PATH.

(1) To list the python path,
     import sys
     print(sys.path)

(2) To insert a path,
    sys.path.insert(0, '{PATH TO INSERT}')

    This enables one to import library from other folder.