简体   繁体   English

如何通过setup.py仅在f2py中包含一些模块

[英]How to include only some modules in f2py via setup.py

I have a very long piece of fortran code, some parts of which I would like to expose to python. 我有很长的fortran代码,我想将其中的某些部分介绍给python。 It works fine from the command line, with 从命令行,它可以正常工作

f2py -m extrpsf -c extract_psf.f95 only: extract_psf psfmany :

I'm hoping to distribute this, so am trying to get it to work with numpy.distutils.core. 我希望分发此文件,因此尝试使其与numpy.distutils.core一起使用。 I thought the following should work: 我认为以下应该工作:

from numpy.distutils.core import setup, Extension
setup(name="extrpsf",
ext_modules=[Extension(name='extrpsf', sources=['extract_psf.f95'],
                       f2py_options=['only: extract_psf psfmany :'])])

Unfortunately, this does not work. 不幸的是,这不起作用。 Any suggestions? 有什么建议么?

Sorry for not having given details about the error message before. 很抱歉以前没有提供有关错误消息的详细信息。 I now did what I should have done before and simplified the problem. 我现在做了我应该做的事情,并简化了问题。 So, assume I have the following fortran code add_test.f95 subroutine add_axis2(in, n1, n2, out) integer, intent(in) :: n1, n2 double precision, intent(in) :: in(n1, n2) double precision, intent(out) :: out(n2) integer :: i2 do i2 = 1,n2 out(i2) = sum(in(:,i2)) enddo return end subroutine add_axis2 因此,假设我有以下fortran代码add_test.f95子例程add_axis2(in,n1,n2,out)整数,intent(in):: n1,n2 double精度,intent(in):: in(n1,n2)double精度,intent(out):: out(n2)整数:: i2 do i2 = 1,n2 out(i2)= sum(in(:,i2))enddo return end子例程add_axis2

subroutine add_axis1(in, n1, n2, out)
  integer, intent(in) :: n1, n2
  double precision, intent(in) :: in(n1, n2)
  double precision, intent(out) :: out(n1)
  integer :: i1
  do i1 = 1,n1
     out(i1) = sum(in(i1,:))
  enddo
  return
end subroutine add_axis1

Then, if I run 那我跑

f2py -m add_test -c add_test.f95 only: add_axis1 :

I get a add_test.so, which, if I import it, only has add_axis1. 我得到一个add_test.so,如果我将其导入,则仅具有add_axis1。 However, if I make a setup.py with 但是,如果我用

from numpy.distutils.core import setup, Extension
setup(name="add_test",
      ext_modules=[Extension(name='add_test', sources=['add_test.f95'],
                             f2py_options=['only: add_axis2 :'])])

and run 并运行

python setup.py build

the resulting add_test.so still has both subroutines. 生成的add_test.so仍然具有两个子例程。 (This also explains the error message I get earlier; there are parts in my longer code that f2py chokes on, which in the manual f2py invocation simply get skipped). (这也解释了我之前得到的错误消息;较长的代码中有f2py阻塞的部分,在手动f2py调用中只是被跳过了)。 So, I guess my question has simplified to how I ensure in the setup.py that only some routines get made. 因此,我想我的问题已简化为如何确保在setup.py中仅执行某些例程。

Looking in more detail at the output, I see that 详细查看输出,我看到

running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "add_test" sources
f2py options: ['only: add_axis2 :']
f2py:> build/src.linux-x86_64-2.7/add_testmodule.c
creating build
creating build/src.linux-x86_64-2.7
IOError: [Errno 2] No such file or directory: 'only: add_axis2 :'. Skipping file "only: add_axis2 :".

so the error is becoming clearer. 因此错误变得越来越明显。 ... and indeed, with this some further googling gives the answer; ……的确,通过进一步的搜索,可以找到答案; from http://cens.ioc.ee/pipermail/f2py-users/2005-July/001125.html if I do instead 如果我改为从http://cens.ioc.ee/pipermail/f2py-users/2005-July/001125.html

from numpy.distutils.core import setup, Extension
setup(name="add_test",
      ext_modules=[Extension(name='add_test', sources=['add_test.f95'],
                             f2py_options=['only:']+['add_axis2']+[':'])])

the simple example works, and with a similar change the more complicated one works too... 简单的示例就可以了,如果进行类似的更改,更复杂的示例也可以...

Great, the solution with using f2py_options works! 太好了,使用f2py_options的解决方案有效!

Extension(
    name='module.routine_ext',
    sources=['src/source_file.f'],
    f2py_options=['only:', 'subroutine_name', ':'],
    language='f77')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM