python调用fortran模块

yipeiwu_com5年前Python基础

在python中调用fortran代码,要用到f2py这个程序。它的项目主页在此。现在该项目已经合并到numpy中了,先安装python再装好numpy,就可以使用f2py。不过对windows平台必须使用gnu的fortran编译器gfortran,在此下载。装完了python,numpy和gfortran这三样东西之后,还必须更改如下几个环境变量:

    1.在$PATH中添加gfortran的路径,我的是c:\Program Files\pythonxy\mingw\bin\

    2.在$PATH中添加python的路径,我的是c:\Python26\

    3.新建环境变量C_INCLUDE_PATH,添加gfortran头文件的路径,我的是c:\Program Files\pythonxy\mingw\include\

好啦现在f2py就可以用了。新建fortran程序foo.f90如下

foo.f90

subroutine hello (a)
 
integer a
 
  write(*,*)'Hello from Fortran90!!!',a
 
end subroutine hello

编译

f2py -m foo -c foo.f90 

运行

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
>>> foo.hello(15)
 Hello from Fortran90!!!     15

另外附上f2py支持的数据类型有

integer[ | *1 | *2 | *4 | *8 ], logical[ | *1 | *2 | *4 | *8 ]
integer*([ -1 | -2 | -4 | -8 ])
character[ | *(*) | *1 | *2 | *3 | ... ]
real[ | *4 | *8 | *16 ], double precision
complex[ | *8 | *16 | *32 ]
<dim> | <start>:<end> | * | :
intent([ in | inout | out | hide | in,out | inout,out | c |
     copy | cache | callback | inplace | aux ])
dimension(<dimspec>)
common, parameter
allocatable
optional, required, external
depend([<names>])
check([<C-booleanexpr>])
note(<LaTeX text>)
usercode, callstatement, callprotoargument, threadsafe, fortranname
pymethoddef
entry

以上所述就是本文的全部内容了,希望大家能够喜欢

相关文章

python pytest进阶之conftest.py详解

前言 前面几篇文章基本上已经了解了pytest 命令使用,收集用例,finxture使用及作用范围,今天简单介绍一下conftest.py文件的作用和实际项目中如是使用此文件! 实例场...

利用pyinstaller或virtualenv将python程序打包详解

运行环境: CentOS6.5_x64 Python版本 : 2.6 使用pyinstaller打包 pyinstaller可以将python程序打包成二进制文件,打包后的文件在没有p...

教你用 Python 实现微信跳一跳(Mac+iOS版)

教你用 Python 实现微信跳一跳(Mac+iOS版)

这几天看网上好多微信跳一跳破解了,不过都是安卓的,无奈苹果不是开源也没办法。这个教程是 Mac + iOS , 要下xcode 要配置环境小白估计是没戏了,有iOS 开发经验的可以看看...

python返回昨天日期的方法

本文实例讲述了python返回昨天日期的方法。分享给大家供大家参考。具体实现方法如下: #-*-coding:utf-8-*- import datetime def getYes...

centos 下面安装python2.7 +pip +mysqld

python2.7 复制代码 代码如下: wget http://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz yum inst...