Note that there are some explanatory texts on larger screens.

plurals
  1. POf2py: Exposing parameters from "used" modules
    primarykey
    data
    text
    <p>I assume that this question has been addressed somewhere, but I have spent an inordinate amount of time looking around for the answer including digging into the source code a bit. I have tried to put the problem in the first paragraph. The rest shows a basic example of the problem.</p> <p>I am attempting to compile a module that contains a <code>USE</code> statement pointing to another, more general, module. I would prefer to keep the used module separate so that it can be used in several "packages" as a set of general settings. When I compile the two modules using f2py everything works as advertised from the fortran side, but from the python side <code>USE</code> appears to be ignored. If I allow f2py to generate a signature file, the file contains a <code>USE</code> statement as is appropriate, but if I complete the compilation and import from the resulting library the parameters from the used module are not available in the module that contains the use statement. Below are two modules illustrating the situation:</p> <pre><code>MODULE test INTEGER, PARAMETER :: a = 1 END MODULE test MODULE test2 USE test INTEGER, PARAMETER :: b = 2 END MODULE test2 </code></pre> <p>In order to show the intermediate step I ran <code>f2py -h test.pyf test.f90 test2.f90</code>. The following signature file is generated; note that the "test2" module contains "use test":</p> <pre><code>! -*- f90 -*- ! Note: the context of this file is case sensitive. python module test ! in interface ! in :test module test ! in :test:test.f90 integer, parameter,optional :: a=1 end module test module test2 ! in :test:test2.f90 use test integer, parameter,optional :: b=2 end module test2 end interface end python module test ! This file was auto-generated with f2py (version:2). ! See http://cens.ioc.ee/projects/f2py2e/ </code></pre> <p>If I now compile with <code>f2py --fcompiler=gfortran -c test.pyf test.f90 test2.f90</code> I obtain test.so (same as running <code>f2py --fcompiler=gfortran -m test -c test.f90 test2.f90</code> without creating the signature file first). Importing from this library in python exposes test.test.a and test.test2.b, but does not expose test.test2.a as can be seen here:</p> <pre><code>In [1]: import test In [2]: print test.test.a 1 In [3]: print test.test2.b 2 In [4]: print test.test2.a --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /users/solbrig/svn_checkouts/inversion/satmet/branches/solbrig/rootpath/data/users /GeoIPS/src/test/&lt;ipython-input-4-bffcf464e408&gt; in &lt;module&gt;() ----&gt; 1 print test.test2.a AttributeError: a </code></pre> <p>Just to illustrate that <code>b</code> is defined properly in test2 from the perspective of fortran, the following code uses test2 and prints both <code>b</code> and <code>b</code>:</p> <pre><code>SUBROUTINE run_test() USE test2 IMPLICIT NONE print *, "a = ", a print *, "b = ", b END SUBROUTINE run_test </code></pre> <p>After compiling with "f2py -m run_test -c test.f90 test2.f90 run_test.f90" and obtaining run_test.so, run_test can be imported in python and works as expected:</p> <pre><code>In [1]: import run_test In [2]: run_test.run_test() a = 1 b = 2 </code></pre> <p>Any help with this issue would be greatly appreciated. </p> <h3><strong>TL;DR</strong> : When an F90 module that contains a <code>USE</code> is compiled by f2py it does not expose the parameters that are defined in the "used" module as attributes in Python.</h3>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload