Python calls the fortran Module

Source: Internet
Author: User
Tags gfortran
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.

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.