开发者

My settings.py is called 4 times - any ideas why?

开发者 https://www.devze.com 2023-02-18 09:36 出处:网络
When I start the local dev server (./manage runserver) the settings.py is run four times. I noticed that, because a error/debug message is printed four times.

When I start the local dev server (./manage runserver) the settings.py is run four times. I noticed that, because a error/debug message is printed four times.

Any ideas how this can come? I don't even have an idea where to start looking. It's important for me because I'm struggling with the setup of Sentry, which doesn't report any errors of the site the way it is installed now.

Update:

I checked the imported module for import settings, there are none in my apps.

Then I added

import traceback; traceback.print_stack(); print

to settings.py. The result is:

File "./manage.py", line 5, in <module>
  import settings # Assumed to be in the same directory.
File "(...)/myapp/settings.py", line 4, in <module>
  import traceback; traceback.print_stack(); print

File "./manage.py", line 12, in <module>
  execute_manager(settings)
(...)
File "(...)/site-packages/django/conf/__init__.py", line 73, in __init__
  mod = importlib.import_module(self.SETTINGS_MODULE)
File "(...)/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "(...)/myapp/../myapp/settings.py", line 4, in <module>开发者_C百科;
  import traceback; traceback.print_stack(); print

File "./manage.py", line 5, in <module>
  import settings # Assumed to be in the same directory.
File "(...)/myapp/settings.py", line 4, in <module>
  import traceback; traceback.print_stack(); print

File "./manage.py", line 12, in <module>
  execute_manager(settings)
(...)
File "(...)/site-packages/django/conf/__init__.py", line 73, in __init__
  mod = importlib.import_module(self.SETTINGS_MODULE)
File "(...)/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "(...)/myapp/../myapp/settings.py", line 4, in <module>
  import traceback; traceback.print_stack(); print

Is that intended behaviour? If not, how can I find my error?


It's being accessed via different entries in sys.path. You should never attempt to import settings yourself; import django.conf.settings instead.


This behavior actually occurs in brand new Django 1.3 Projects. When the development server is started for the first time, it does four imports:

$ django-admin.py startproject test_project
$ cd test_project/

# settings.py
print "importing settings.py"
...

$ python manage.py runserver
importing settings.py
importing settings.py
importing settings.py
importing settings.py
Validating models...

0 errors found
Django version 1.3, using settings 'test_project.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

If you edit a file with the development server running, it only does two imports:

$ touch settings.py

Output:

importing settings.py
importing settings.py
Validating models...

0 errors found
Django version 1.3, using settings 'test_project.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

I'm not sure why it does this, or if it does it on non-development servers. But the behavior does appear to be normal.


If you track imports in settings.py, it doesn't persist values across the multiple imports:

# settings.py
import_count = 0
import_count += 1
print "import count: %d" % import_count

Fresh start:

$ python manage.py runserver
import count: 1
import count: 1
import count: 1
import count: 1
Validating models...

Changed a file:

import count: 1
import count: 1
Validating models...

So maybe it's just reading the file to configure itself, and verify that everything is OK, before importing it for the final time and actually using it?

0

精彩评论

暂无评论...
验证码 换一张
取 消