title: How to include only some modules in f2py via setup.py
body: <p>I have a very long piece of fortran code, some parts of which I would like to expose to python. It works fine from the command line, with</p>
  
  <pre><code>f2py -m extrpsf -c extract_psf.f95 only: extract_psf psfmany :
  </code></pre>
  
  <p>I'm hoping to distribute this, so am trying to get it to work with numpy.distutils.core.  I thought the following should work:</p>
  
  <pre><code>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 :'])])
  </code></pre>
  
  <p>Unfortunately, this does not work. Any suggestions?</p>
  
  <p>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</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Then, if I run</p>
  
  <pre><code>f2py -m add_test -c add_test.f95 only: add_axis1 :
  </code></pre>
  
  <p>I get a add_test.so, which, if I import it, only has add_axis1. However, if I make a setup.py with</p>
  
  <pre><code>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 :'])])
  </code></pre>
  
  <p>and run</p>
  
  <pre><code>python setup.py build
  </code></pre>
  
  <p>the resulting add_test.so still has both subroutines. (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). So, I guess my question has simplified to how I ensure in the setup.py that only some routines get made.</p>
  
  <p>Looking in more detail at the output, I see that</p>
  
  <pre><code>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:&gt; 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 :".
  </code></pre>
  
  <p>so the error is becoming clearer.
  ...
  and indeed, with this some further googling gives the answer; from
  <a href="http://cens.ioc.ee/pipermail/f2py-users/2005-July/001125.html" rel="nofollow">http://cens.ioc.ee/pipermail/f2py-users/2005-July/001125.html</a>
  if I do instead</p>
  
  <pre><code>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']+[':'])])
  </code></pre>
  
  <p>the simple example works, and with a similar change the more complicated one works too...</p>