0

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.

5
  • " am doing something wrong." Why? What happens? Any error messages? What exactly have you tried? Which operation system do you have? Any particular Python distribution? (Anaconda or something else?) Jan 26, 2017 at 21:56
  • No im just trying the simple example like creating a fibonaci script in fortran and trying to load it in python just to learn how to use f2py because I will need to use it later. im getting this: runfile('F:/SLB/Larryf2py/teste.py', wdir='F:/SLB/Larryf2py') File "F:/SLB/Larryf2py/teste.py", line 9 f2py -c fib1.f -m fib1 ^ SyntaxError: invalid syntax however fib1.f is the name of my script in fortran so should work right? but I read about shellsomething and has to do with directory... Dont know if im doing right. Jan 26, 2017 at 22:19
  • Show the code of your programs and full error messages. It is not clear when the error is printed. Read How to Ask And please answer my questions from the previous comment. Edit your question to add more information, don't use comments for that. Jan 26, 2017 at 22:57
  • Now I added all info that I have, thanks! Jan 27, 2017 at 0:25
  • Please don't put greetings like "regards, Patrick Thanks in advance!" in your questions. We don't do that here, we delete this. Your name is appended automatically under your question with your photo. Jan 27, 2017 at 7:30

4 Answers 4

2

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.

3
  • My Os is windows. I tried to execute in CMD.exe in the folder of my fortran file but still not working. "the error is f2py is not recognized as an internar or external command, operable or batch file" I also tried with numpy.f2py because f2py is now part of numpy but the same error returned. Do I have to compile F2py? I download anaconda 2.5 and python 2.7.11 it should be inside that. I can easily import numpy.f2py in the spyder. Jan 27, 2017 at 14:00
  • You can follow the advice of @vladimir-f, official Python documents for settings on Windows machines are here. Probably your installation is suffering from bad/wrong "environment variables".
    – trblnc
    Jan 27, 2017 at 14:38
  • Other possibilities might be installing a virtual machine, just an example , or dual boot if you can/like to run your code with a linux distro. I suggested these 2 options as Linux package managers should set the "environment variables" seamlessly. All these methods should work more or less equivalently once they are set correctly.
    – trblnc
    Jan 27, 2017 at 14:38
2

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.

1

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.

0

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.

3
  • My Os is windows. I tried to execute in CMD.exe in the folder of my fortran file but still not working. "the error is f2py is not recognized as an internar or external command, operable or batch file" I also tried with numpy.f2py because f2py is now part of numpy but the same error returned. Do I have to compile F2py? I download anaconda 2.5 and python 2.7.11 it should be inside that. I can easily import numpy.f2py in the spyder. Jan 27, 2017 at 13:52
  • @PatrickMachado I assume you can't even run python.exe from the command line. See stackoverflow.com/a/37918326/721644 Jan 27, 2017 at 14:06
  • I added the extensions and nothing happened. I cannot load the f2py or numpy.f2py in the CMD. Not sure why. I installed Anaconda in the F: directory and my windows is at c: could it be the cause?, besides that python I can load in the cmd Jan 27, 2017 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.