【问题标题】:getting module has no attribute error after compiling with f2py用f2py编译后获取模块没有属性错误
【发布时间】:2021-01-12 16:58:30
【问题描述】:

我在尝试使用 f2py(在 gfortran 中)使一堆 Fortran 代码在 Python 中工作时遇到了一个问题。我的 Fortran 代码有一大堆子例程,它在 gfortran 和在线虚拟 fortran IDE 中都能很好地编译。 我遇到问题的子程序是一个合并排序子程序(我从 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 代码(在 jupyter 笔记本中)的开头插入 import fprogram,我想在其中使用它(我知道原始是一个列表,它不是尺寸问题):

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

我收到了错误消息

module 'fprogram' has no attribute 'MSort'

同时,当我以同样的方式使用 fprogram 中的任何其他子例程时,它可以完美运行。 我已经将 fortran 代码修改为没有带有 intent(inout) 的变量,因为在以前的情况下这已经解决了我的问题,但这次它没有工作。我该怎么做才能让 python 识别这个子程序?

【问题讨论】:

    标签: python fortran gfortran f2py


    【解决方案1】:

    有 2 个不同的错误。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-03
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多