Fortran Routine calling a Python Function

Click For Summary
SUMMARY

This discussion centers on the integration of Python with Fortran, specifically the challenges of calling a Python function from a Fortran subroutine. The user initially attempted to use f2py but found it more practical to restructure their Fortran program to interface with Python instead. They referenced mixed-language programming resources, noting that while calling C functions from Fortran is documented, calling Python directly presents significant challenges due to Python's interpreted nature. Ultimately, the user successfully transitioned their Fortran code to work with Python, highlighting the efficiency and ease of using Python for GUI development alongside Fortran.

PREREQUISITES
  • Understanding of Fortran programming, specifically subroutine structure
  • Familiarity with Python programming and its function calling conventions
  • Knowledge of f2py for interfacing Python with Fortran
  • Basic concepts of mixed-language programming
NEXT STEPS
  • Research how to use f2py for Fortran-Python integration
  • Explore techniques for compiling Python code to enhance interoperability with Fortran
  • Learn about creating GUIs in Python, especially using libraries like Tkinter or PyQt
  • Investigate other mixed-language programming approaches, particularly involving C and Fortran
USEFUL FOR

Developers working with Fortran who wish to leverage Python for enhanced functionality, particularly in GUI development, as well as those interested in mixed-language programming techniques.

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!
 

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