如何在 python 中使用 f2py 模块

How to Use f2py module in python

只是在使用 f2py 时遇到了一些麻烦。我已经将以下代码编译成一个 .so 文件就好了,它可以很好地导入我的 python 代码,但我只是想知道我该如何使用它。我知道有一个命令 doc 但它只是说这个模块没有名为“doc”的属性

Fortran 代码:

subroutine Test
implicit none
real, dimension(3600000) :: Alpha,Sigma
open(10, file='Alpha.txt')
read(10, *) Alpha
Sigma = (87.6*2)/((87.6*(sin(Alpha))**2)+(2*cos(Alpha)**2))
end subroutine

Python代码:

import matplotlib.pyplot as plt 
import numpy as np 
import Sigma

print(Sigma._doc_)

错误:

File "/home/tom/Desktop/f2py/Plot.py", line 5, in <module>
    print(Sigma._doc_)
AttributeError: 'module' object has no attribute '_doc_'

我是否必须以某种方式将 doc 属性输入到原始 Fortran 代码中?如果是这样,我该怎么做?

根据他们的 example,您似乎需要在 module.subroutine.__doc__ 上调用打印。

如果我把你的文件保存到test1.f90,然后编译它

f2py -c test1.f90 -m test1

然后查询文档字符串。

~$ python
Python 3.6.4 (default, Jan  5 2018, 02:35:40) 
[GCC 7.2.1 20171224] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import test1
>>> print(test1.test.__doc__)
test()

Wrapper for ``test``.


>>>

这对我来说很好,因为你没有论据去 Test