tags:

views:

73

answers:

2

I put

print 'Hello world!'

into __init__.py in my django project. When I run ./manage.py runserver now, I get

gruszczy@gruszczy-laptop:~/Programy/project$ ./manage.py runserver
Hello world!
Hello world!
Validating models...
0 errors found

Why is __init__.py run twice? It should be loaded only once.

+2  A: 

It should be loaded only once... per process. I'm guessing that manage.py forks, and that two separate processes are launched. Could you print the result of os.getpid()?

Thomas
Indeed, you are right. Thanks a lot :-)
gruszczy
A: 

One simple way of finding out would be to raise an exception. Perhaps something like this in your init.py:

import os
if os.path.isfile('/tmp/once.log'):
    raise Exception
open('/tmp/once.log','w').write('first time')

Then you can inspect the traceback.

Peter Bengtsson