简体   繁体   中英

How to execute `f2py`?

How can I wrap in the f2py module?

I mean, I am reading a few tutorials that say I should execute

f2py FIB1.f -m FIB2 -h FIB1.pyf

However, I don't know where I have to execute that, for sure not in spyder or I am doing something wrong.

Why?

Because I execute this code that should create the extension module of Fortran with Python from my subroutine in Fortran, however an error is generated.

  • my Fortran subroutine:

     SUBROUTINE FIB(A,N) INTEGER N REAL*8 A(N) DO I=1,N IF (I.EQ.1) THEN A(I) = 0.0D0 ELSEIF (I.EQ.2) THEN A(I) = 1.0D0 ELSE A(I) = A(I-1) + A(I-2) ENDIF ENDDO END 

What I'm executing in Python:

import numpy.f2py as f2py

f2py FIB1.f -m FIB2 -h FIB1.pyf

The error is this one:

runfile('F:/SLB/Larryf2py/teste.py', wdir='F:/SLB/Larryf2py')
  File "F:/SLB/Larryf2py/teste.py", line 9
    f2py FIB1.f -m FIB2 -h FIB1.pyf
            ^
SyntaxError: invalid syntax

As far as I know, not sure, It should generate something like:

# File setup.py
def configuration(parent_package='',top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration('',parent_package,top_path)

    config.add_extension('m',
                         sources = ['m.pyf','foo.c'])
    return config
if __name__ == "__main__":
    from numpy.distutils.core import setup
    setup(**configuration(top_path='').todict())

This example of what is generated is to C but I think its something like that to Fortran too.

What I think? That I should run the first code in another place of the Python...

I tried to reproduce this .

Are you adding f2py command inside your Python code? If yes, that's not good.

The line f2py FIB1.f -m FIB2 -h FIB1.pyf needs to be in command line, not inside any *.py script.

From F2PY Users Guide and Reference Manual f2py is a program/compiler from The purpose of the F2PY –Fortran to Python interface generator– project is to provide a connection between Python and Fortran languages. F2PY is a Python package (with a command line tool f2py and a module f2py2e) that facilitates creating/building Python C/API extension modules that make it possible.

In additon, here is detailed explanation on how to use f2py .

There might be some other problems in OP's question but for the moment most vital is this one. Like the Fortran subrotuine is not using implicit none , etc.

Well I found an answer.

Looks like in this version of anaconda one is supossed tu put like

Python c:\user\anaconda3\scripts\f2py.py FIB1.f -m FIB2 -h FIB1.py

so this way that f2py.py part was substituted for all of that. For sure I'll have more trouble in the future with this module but so far my doubts are clear.

If you want to convert fortran into a python object using a python code, the following will work:

 from numpy import f2py

 with open('path_to_fotran_code') as sourcefile:
     sourcecode = sourcefile.read()

 f2py.compile(sourcecode, modulename='test_module', verbose=1,
              extra_args= '--verbose'
                          '--compiler=mingw32')
 import test_module

In case you do not have mingw32 you can use --compile=msvc (I ran into problems trying to use msvc which I could never solve with all the internet help).
Also ensure that your windows path environment is configured to point the fortran compiler path.

f2py is not a Python command, you cannot execute it in the Python shell or inside a .py source file. It is an executable command. You must execute it in your system's shell.

You still did not answer which operating system you have, but if it is Windows, you must run it in the CMD.exe command prompt or in PowerShell. If it is Linux or similar, run it in bash or similar shell. You must run it in the same directory (folder), where the Fortran source file is located.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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