2011-03-18 85 views
2

當我啓動本地開發服務器(./manage runserver)時,settings.py會運行四次。我注意到,因爲錯誤/調試消息被打印了四次。我的settings.py被調用4次 - 任何想法爲什麼?

任何想法如何能來?我什至不知道從哪裏開始尋找。這對我很重要,因爲我正在努力設置Sentry,它不會像現在那樣報告站點的任何錯誤。

更新:
我檢查了導入模塊import settings,在我的應用程序中沒有。

然後我說

import traceback; traceback.print_stack(); print 

到settings.py中。其結果是:

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 

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 

這是預期的行爲?如果不是,我怎麼能找到我的錯誤?

回答

3

它通過sys.path中的不同條目進行訪問。你不應該試圖自己導入settings;改爲導入django.conf.settings

2

這種行爲實際上發生在全新的Django 1.3項目中。當開發服務器啓動的第一次,它4進口

$ 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. 

如果編輯與開發服務器運行的文件,它只做兩名外援

$ touch 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. 

我不知道爲什麼這樣做,或者如果這樣做,對非發展小號ervers。但這種行爲似乎是正常的。


如果您追蹤settings.py進口,它不跨多個進口持續值:

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

新鮮的開始:

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

修改的文件:

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

所以也許它是ju st讀取文件進行自我配置,然後確認一切正常,然後再導入最終時間並實際使用它?

+1

非常感謝您的分析和確認。我必須在幾天內回到這個問題。關於import_count只是一個想法:我認爲它應該總是返回1,如果你分配0,然後加1。可選方法:'try:import_count + = 1,除了NameError:import_count = 0'。再次感謝您幫助我瞭解settings.py ... – jammon 2011-06-29 18:58:08

相關問題