This article introduces how to call the fortran code in Python. It mainly uses the f2py program, which is very practical. If you need it, you can refer to calling the fortran code in python, f2py is used. Its project home page is here. Now the project has been merged into numpy. Install python and then install numpy to use f2py. However, for windows, you must use the gnu fortran compiler gfortran, Which is downloaded here. After installing python, numpy, and gfortran, you must change the following environment variables:
1. Add the gfortran PATH to $ PATH. my options are c: \ Program Files \ pythonxy \ mingw \ bin \
2. Add the python PATH to $ PATH. My PATH is c: \ Python26 \
3. create the environment variable C_INCLUDE_PATH and add the path of the gfortran header file. my options are c: \ Program Files \ pythonxy \ mingw \ include \
Now f2py can be used. Create the fortran Program foo. f90 as follows:
Foo. f90
subroutine hello (a) integer a write(*,*)'Hello from Fortran90!!!',a end subroutine hello
Compile
f2py -m foo -c foo.f90
Run
$ pythonPython 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import foo>>> foo.hello(15) Hello from Fortran90!!! 15
Additionally, f2py supports the following data types:
integer[ | *1 | *2 | *4 | *8 ], logical[ | *1 | *2 | *4 | *8 ]integer*([ -1 | -2 | -4 | -8 ])character[ | *(*) | *1 | *2 | *3 | ... ]real[ | *4 | *8 | *16 ], double precisioncomplex[ | *8 | *16 | *32 ]
|
:
| * | :intent([ in | inout | out | hide | in,out | inout,out | c | copy | cache | callback | inplace | aux ])dimension(
)common, parameterallocatableoptional, required, externaldepend([
])check([
])note(
)usercode, callstatement, callprotoargument, threadsafe, fortrannamepymethoddefentry
The above is all the content of this article. I hope you will like it.