Python Fortran Routine calling a Python Function

Click For Summary
Merging Python with Fortran for enhanced performance and usability has proven challenging, particularly in calling Python functions from Fortran. The discussion highlights the use of f2py for interfacing but emphasizes the difficulty of invoking Python directly from Fortran, as Python is typically interpreted rather than compiled. A referenced article discusses calling C functions from Fortran, suggesting that a similar approach for Python may not be straightforward. Ultimately, the user shifted their approach by adapting their Fortran program to work with Python instead, finding the transition to be efficient and resulting in a positive experience, especially for GUI development, which was completed in a single afternoon.
LucasCampos
Messages
17
Reaction score
0
I have been trying to merge some python with fortran, to build a nice interface but still be fast. I spent the last two days learning how to use f2py, buy everything would be so much easier if I could call a Python function from Fortran instead of doing the other way around.

Anyone know how to do something like that, if it's even possible?

For example
Code:
Subroutine Example (var1,var2)
Implicit None
Real, Intent(in) :: var1,var2
Real :: var3,var4
command1
command2
.
.
.
pythonfunction(var3,var4)
.
.
.
commandn
EndSubroutine
 
Technology news on Phys.org
I found several articles that discussed fortran mixed-languague programming (my search string was 'fortran "mixed language" programming'. One article that showed up showed how to call a C function from fortran (http://www.vni.com/company/whitepapers/StandardizedMixedLanguageProgrammingforCandFortran.pdf , page 10).

It might be more difficult to call a python function from fortran, unless the python code could be compiled, instead of being interpreted, which is the usual case, I believe.
 
Last edited by a moderator:
Thanks for the answer, but I've given up all hope to call python from fortran. I remade my fortran program to be used with python. Took me the whole afternoon, far less than I expected, and the result was quite nice.

Python is quite a cool language to use with fortran. I wouldn't even think of doing a GUI on fortran, and on python took me just one afternoon, learning included!
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K