Python calls the fortran module and pythonfortran module.

Source: Internet
Author: User
Tags gfortran

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.