Python calls the fortran module and pythonfortran module.
The f2py program is used to call the fortran code in python. 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 ]<dim> | <start>:<end> | * | :intent([ in | inout | out | hide | in,out | inout,out | c | copy | cache | callback | inplace | aux ])dimension(<dimspec>)common, parameterallocatableoptional, required, externaldepend([<names>])check([<C-booleanexpr>])note(<LaTeX text>)usercode, callstatement, callprotoargument, threadsafe, fortrannamepymethoddefentry
The above is all the content of this article. I hope you will like it.
Articles you may be interested in:
- Detailed description of Python custom function creation, calling, and function parameters
- Example of using python to call a browser and open a URL
- How Python calls the C/C ++ Dynamic Link Library
- Python implementation subclass calls the parent class Method
- Methods and instances for calling system commands, Shell scripts, and Python scripts in Nodejs
- An example of how to use Python to call the System Shell in Linux