# Converting Python code to Fortran code

**URL:** <https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806>\
**Category:** Help\
**Created:** [June 20, 2022, 8:46am UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806 "2022-06-20T08:46:59Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![hayatullah](https://avatars.discourse-cdn.com/v4/letter/h/e36b37/32.png) [@hayatullah](https://fortran-lang.discourse.group/u/hayatullah)\
**Post date:** [June 20, 2022, 8:46am UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/1 "2022-06-20T08:46:59Z")

</div>

Can we convert Python code to Fortran code. If yes how?

Please guide me I will be very thankful

---

<div class="post-metadata">

**Author:** ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)\
**Post date:** [June 20, 2022, 9:01am UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/2 "2022-06-20T09:01:44Z")

</div>

It depends.

You have some options:

- rewrite everything line by line (very time-consuming, especially if your Python code uses many libraries)
- identify the bottlenecks of your code and only convert them to Fortran
  - one way to do this is to compile a Fortran module to be usable by Python (f2py3)
  - or you can use libraries, which already use C or Fortran, for example SciPy or NumPy

If your code uses NumPy arrays, you should be able to translate it easily to Fortran, because the NumPy syntax is heavily based on Fortran array operations.

---

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [June 20, 2022, 9:40am UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/3 "2022-06-20T09:40:53Z")

</div>

As said already, if your code uses array operations, try changing that part to Fortran.  
Also, If you want to change the whole python code (if it is not time-consuming and worth the effort), try to develop a similarity between the python and Fortran statements. To start with try this website to have a comparison between Fortran and python ([Python Fortran Rosetta Stone — Fortran90 1.0 documentation](https://www.fortran90.org/src/rosetta.html)). I personally find this comparison very useful.

bests of luck!!

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [June 20, 2022, 11:11am UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/4 "2022-06-20T11:11:43Z")

</div>

My list of Fortran tools has a section on [Translation to Fortran](https://github.com/Beliavsky/Fortran-Tools#translation-to-fortran), where [pyccel](https://github.com/pyccel/pyccel) is mentioned. The Readme says

> **Pyccel** stands for Python extension language using accelerators.
> 
> The aim of **Pyccel** is to provide a simple way to generate automatically, parallel low level code. The main uses would be:
> 
> 1. Convert a _Python_ code (or project) into a Fortran or C code.
> 2. Accelerate _Python_ functions by converting them to _Fortran_ or _C_ functions.
> 
> **Pyccel** can be viewed as:
> 
> - _Python-to-Fortran/C_ converter
> - a compiler for a _Domain Specific Language_ with _Python_ syntax
> 
> Pyccel comes with a selection of **extensions** allowing you to convert calls to some specific python packages to Fortran/C. The following packages will be covered (partially):
> 
> - numpy
> - scipy
> - mpi4py (not available yet)
> - h5py (not available yet)
> 
> Pyccel’s acceleration capabilities lead to much faster code. A small speed comparison of python vs pyccel or other tools can be found in the [performance](https://github.com/pyccel/pyccel/blob/master/performance.md) file.

I wrote about it in a December 2020 thread [Translating Python to Fortran with pyccel](https://fortran-lang.discourse.group/t/translating-python-to-fortran-with-pyccel/503) – it has become more capable since then. Pyccel is an active project with numerous contributors. It would be great if translation to Fortran became a standard way of speeding up Python code, especially scientific code. If many Fortranners tried it and gave feedback to the Pyccel developers, that would benefit both the scientific Python and Fortran communities. They were responsive to a few issues I raised.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [October 1, 2022, 1:51pm UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/5 "2022-10-01T13:51:36Z")

</div>

Last year, a minibook _Fortran Best Practice_ was imported from [fortran90.org](http://fortran90.org) (@certik) to [fortran-lang.org](https://fortran-lang.org/en/learn/)

I remember we also considered importing the [Python Fortran Rosetta Stone](https://www.fortran90.org/src/rosetta.html) but were stopped by technical problems to keep the layout of the tables. Does the Sphinx new framework changes something? (@awvwgk @henilp105)

See: [Transfer fortran90.org content · Issue #112 · fortran-lang/webpage · GitHub](https://github.com/fortran-lang/webpage/issues/112#issuecomment-1220651825)

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [October 1, 2022, 1:55pm UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/6 "2022-10-01T13:55:26Z")

</div>

With the new framework you should be able to just copy over the [fortran90.org](http://fortran90.org) files without requiring any change as we can also generate pages from rst directly. This should allow to get a quick overview on how the pages will look like on [fortran-lang.org](http://fortran-lang.org).

Since we want a consistent source format, the pages should still be converted to markdown at some point.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [October 1, 2022, 1:59pm UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/7 "2022-10-01T13:59:02Z")

</div>

For markdown, @certik suggested on GitHub we use MyST markdown.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [October 1, 2022, 1:59pm UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/8 "2022-10-01T13:59:59Z")

</div>

Indeed, we are already using MyST markdown for the new page. So everything that is possible in reStructuredText, is also possible in markdown there.

---

<div class="post-metadata">

**Author:** ![henilp105](https://avatars.discourse-cdn.com/v4/letter/h/919ad9/32.png) [@henilp105](https://fortran-lang.discourse.group/u/henilp105)\
**Post date:** [October 1, 2022, 2:44pm UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/9 "2022-10-01T14:44:20Z")

</div>

@vmagnin I tried to use the rst directly and it looks great , extremely similar to the original site, but for using myst markdown ( pandoc produces unuseful output) we would have to manually rewrite the entire document to include in the learn section.

CC @awvwgk

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [October 1, 2022, 4:21pm UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/10 "2022-10-01T16:21:25Z")

</div>

Did you try with the command I used for _Fortran Best Practice_?

```bash
$ pandoc --standalone --toc --resource-path=../images --to markdown_strict+pipe_tables+backtick_code_blocks best-practices.rst -o best-practices.md

```

It did a good job. But for Rosetta, the problem was the tables for code comparison.

---

<div class="post-metadata">

**Author:** ![henilp105](https://avatars.discourse-cdn.com/v4/letter/h/919ad9/32.png) [@henilp105](https://fortran-lang.discourse.group/u/henilp105)\
**Post date:** [October 1, 2022, 4:34pm UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/11 "2022-10-01T16:34:27Z")

</div>

@vmagnin I had tried with only markdown , I would have to retry with the markdown strict and flags.  
Thanks

---

<div class="post-metadata">

**Author:** ![henilp105](https://avatars.discourse-cdn.com/v4/letter/h/919ad9/32.png) [@henilp105](https://fortran-lang.discourse.group/u/henilp105)\
**Post date:** [October 2, 2022, 3:25am UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/12 "2022-10-02T03:25:35Z")

</div>

Thanks Sir, Resolved it and Opened an PR for the same.

> <https://github.com/fortran-lang/webpage/pull/175>
>
> This PR adds Fortran90 content Python Fortran Rosetta Stone, We are grateful to …the original owners (@certik) of the content for opensourcing it . This content has been taken from https://github.com/certik/fortran90.org . This content has been formatted with pandoc and converted to myst markdown for consistency.
> 
> This PR Resolves:
> 
> Closes #112
> 
> Thanks and Regards,
> Henil
> 
> CC @awvwgk @vmagnin

Thanks and Regards,  
Henil

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 2, 2022, 10:50pm UTC](https://fortran-lang.discourse.group/t/converting-python-code-to-fortran-code/3806/13 "2022-10-02T22:50:52Z")

</div>

Thanks for resurrecting this. Yes, it would be cool to port this.
