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

2024-09-19 23:40:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试使用f2py(在gfortran中)在Python中使用Fortran代码时遇到了一个问题。我的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识别这个子例程


Tags: sizeifcalloutrealelseworkend
1条回答
网友
1楼 · 发布于 2024-09-19 23:40:59

有两种不同的错误

首先,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)

相关问题 更多 >