ephraim
ephraim

Reputation: 436

import fortran object into Python

I'm trying to compile some project I've found on the web. the project is wrapping some Fortran code into python object.

The author of that project wrote I need to run the setup.py file.

I've done that. It contains the following piece of code:

  ext = Extension(name = "GaussianFitter._Fitter",
                sources = ["GaussianFitter/src/lmdif.f",
                           "GaussianFitter/src/splev.f",
                           "GaussianFitter/src/gaussian.f90"],

and obviously all these "f" files are existing. When I install that setup.py file it seams to do some job succesfully but the first line in the script which uses that _Fitter:

import _Fitter

doesn't work.

I'm newby with python, and have no experiment with Fortran at all, so please forgive my ignorance.

Edit: project available here:

https://github.com/ardiloot/GaussianFitter

Thanks!

Upvotes: 1

Views: 459

Answers (1)

ephraim
ephraim

Reputation: 436

Finally made it, so here's my for-begginers tutorial:

note - since it's for beginners - it sets all in the global scope.

  1. install mingw32 from: http://www.mingw.org/ Add it's bin path to system PATH.
  2. add a setup.cfg file containing the following text:

    [build]

    compiler=mingw32

locate it in %pythondir%\Lib\distutils

  1. since setup.py file contains links to .c or .f files (c++/fortran/c) in order to make command (python setup.py install) be able to locate these files - run the command when working directory (i.e. cd c:....\) is set.

  2. Ignore red error line in code when you import that module. It is in the LIB directory, just try to run...

EDIT:

If still not managing to import the already built f files - building it in the following way might help:

python setup.py build_ext --inplace

which generates an object (*.pyd), this helped me after it, for any reason, stopped working.

Upvotes: 1

Related Questions