0

I have a simple subroutine written in Fortran. Just for testing reasons it is as simple as this:

!test.f90
subroutine test()
end subroutine test

It compiles well with gfortran and with f2py. I compile it with f2py like so:

$ f2py -m test -c test.f90

If I go to python, I can import it and everything works ok:

>>> import test
>>> test.test() # ok. nothing as expected and with no errors

In django I have an app called just myapp. It has a view which looks like this:

from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext, loader
from numpy import *
from test import *  # the module itself imports ok, I get no errors

def index(request):
    template = loader.get_template('myapp/index.html')
    #test.test() # but if I try to call a function from it, I get an error
    context = RequestContext(request,{})
    return HttpResponse(template.render(context))

The error message is: AttrubuteError at /myapp/ test. PS. I'm a python and django newbee, so I guess I could make some "stupid" error.

EDIT

Now, thanks to laike9m, I manage to call a fortran function, at least I do not have error any longer. But at the same time it seems as if the function does nothing. I mean this. I changed my fortran function a little bit, so that it now writes some stuff to a file. If I check it in pure python, I see that it works - a file is modified. But if I call it in django, it does not modify it. So, my codes now look like this:

!fortran code
!test.f90
subroutine test()
  open(1,file='test.txt',status='replace')
  write(1,*) "Hello, Django! My name is Fortran."
  close(1)
end subroutine test


from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext, loader
from numpy import *
from test import * # it's ok

def index(request):
    template = loader.get_template('myapp/index.html')
    test() # no error any longer, but the function itself does nothing
    context = RequestContext(request,{})
    return HttpResponse(template.render(context))

So, my question is how to check whether fortran module works or not?

EDIT

Thanks to laike9m and Vladimir, I finally did it. The problem was in the path to the file. I had to put the file right in the root folder of the project.

3
  • Don't use unit number 1 or anything smaller than 11. They can have a special purpose and be pre-connected. Commented Oct 21, 2014 at 12:44
  • Thanks, Vladimir, I will check it now Commented Oct 21, 2014 at 12:46
  • @Vladimir Unfortunately, that does not help. It still has no effect Commented Oct 21, 2014 at 12:50

1 Answer 1

1

If you import test, then it's test.test()
If you from test import *, then it's test()

See The import system

Sign up to request clarification or add additional context in comments.

8 Comments

Could you, please, have a look at my updated question?
@Jacobian I'm sorry but I'm not familiar with fotran. Is it working when you type in REPL?
@Jacobian You asked how to check whether fortran module works or not?, but this is just what I want to ask.
I mean it works in python, when I go right to console and type import test.... but in Django, when I call the very same function, it has no effect, it just does nothing.
@Jacobian So what's the effect of test()?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.