使用f2py编译后,geting模块没有属性错误



我在尝试使用f2py(在gfortran中(使一堆Fortran代码在Python中工作时遇到了一个问题。我的Fortran代码有一大堆子例程,它在gfortran和在线虚拟Fortran IDE中都能很好地编译。我遇到问题的子程序是一个mergesort子程序(我从rosettastone中提取并修改了它(,它看起来像这样:

subroutine MSort(N, A, A_out)
implicit none
integer, intent(in) :: N 
real, dimension(N), intent(in) :: A
real, dimension(N), intent(out) :: A_out
real :: work((size(A) + 1) / 2)
A_out=A
call MergeSort(A_out, work)
contains
subroutine merge(A, B, C)
implicit none
real, target, intent(in) :: A(:), B(:)
real, target, intent(inout) :: C(:)
integer :: i, j, k
if (size(A) + size(B) > size(C)) then
stop
end if
i = 1; j = 1
do k = 1, size(C)
if (i <= size(A) .and. j <= size(B)) then
if (A(i) <= B(j)) then
C(k) = A(i)
i = i + 1
else
C(k) = B(j)
j = j + 1
end if
else if (i <= size(A)) then
C(k) = A(i)
i = i + 1
else if (j <= size(B)) then
C(k) = B(j)
j = j + 1
end if
end do
end subroutine merge
subroutine swap(x, y)
implicit none
real, intent(inout) :: x, y
real :: tmp
tmp = x; x = y; y = tmp
end subroutine
recursive subroutine MergeSort(A, work)
implicit none
real, intent(inout) :: A(:)
real, intent(inout) :: work(:)
integer :: half
half = (size(A) + 1) / 2
if (size(A) < 2) then
continue
else if (size(A) == 2) then
if (A(1) > A(2)) then
call swap(A(1), A(2))
end if
else
call MergeSort(A( : half), work)
call MergeSort(A(half + 1 :), work)
if (A(half) > A(half + 1)) then
work(1 : half) = A(1 : half)
call merge(work(1 : half), A(half + 1:), A)
endif
end if
end subroutine MergeSort
end subroutine MSort

我用编译的

$ f2py -c -m fprogram fprogram.f90

并在我的python代码的开头插入了import fprogram(在jupyter笔记本中(,我想这样使用它(我知道原来是一个列表,它不是维度问题(:

size=len(original_list)
sorted_list=fprogram.MSort(size,original_list)

我收到错误信息

module 'fprogram' has no attribute 'MSort'

同时,当我以同样的方式使用fprogram中的任何其他子例程时,它都能完美地工作。我已经修改了fortran代码,使其不包含intent(inout)变量,因为在以前的案例中,这已经解决了我的问题,但这次没有起作用。我能做些什么让python重新配置这个子程序?

有两个不同的错误。

首先,f2py创建所有小写过程。因此,您需要调用fprogram.msort,这正是错误消息试图告诉您的。

另一方面,f2py发现参数N在python中是多余的。这就是为什么它创建了一个函数,其中N是一个可选的参数中心。因此,您还需要以以下方式之一调用

sorted_list=fprogram.msort(original_list)
sorted_list=fprogram.msort(original_list, size)
sorted_list=fprogram.msort(original_list, n=size)

我是怎么发现的?我通过调用(在python REPL中(来阅读文档

import fprogram
help(fprogram)

最新更新