Python Fortran Routine calling a Python Function

AI Thread 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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
19
Views
2K
Replies
8
Views
4K
Replies
7
Views
2K
Replies
2
Views
2K
Replies
9
Views
5K
Replies
2
Views
2K
Replies
6
Views
3K
Back
Top