2016-03-23 31 views
1
#!/usr/bin/env python 
# coding: utf-8 

import os, sys, subprocess, time, re, ast 

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webapi.server.project.settings") 

import django 
django.setup() 

from django.apps import apps 

嘗試:AttributeError:'module'對象沒有屬性'setup'; Django的安裝在一個項目上工作,但沒有其他

cchilders: ./write_creation_tests.py 
Traceback (most recent call last): 
    File "./write_creation_tests.py", line 17, in <module> 
    django.setup() 
AttributeError: 'module' object has no attribute 'setup 

如果我刪除的設置嘗試我無法導入應用程序:

#!/usr/bin/env python 
# coding: utf-8 

import os, sys, subprocess, time, re, ast 

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webapi.server.project.settings") 

from django.apps import apps 

嘗試:

cchilders: ./write_creation_tests.py 
Traceback (most recent call last): 
    File "./write_creation_tests.py", line 19, in <module> 
    from django.apps import apps 
ImportError: No module named apps 

in manage.py:

if __name__ == "__main__": 
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webapi.server.project.settings") 

因此我的os.environ設置與另一個項目中的格式匹配。我在其他django項目中以相同的方式運行此腳本,它可以工作,但不能在我的webapi中運行。這些項目在pythonpath上。我如何設置Django?謝謝

+1

錯誤表明你正在運行的Django 1.6或以上,在這種情況下,你不這樣做需要調用'setup()'。 – knbk

+0

Django 1.7中增加了'django.apps'模塊。您需要升級Django或更改腳本以使用您的Django版本。建議升級,因爲1.7及更高版本現在已停止使用,並且不會收到安全修復程序。 – Alasdair

+0

沒有設置我得到ImportError:沒有名爲apps的模塊 – codyc4321

回答

2

您需要將您的Django版本升級到支持的版本。較新版本的Django具有setup功能。見list of supported Django versions

答案是點評:

The error suggests that you're running Django 1.6 or older, in which case you don't need to call setup(). – knbk Mar 23 '16 at 16:14

The django.apps module was added in Django 1.7. You'll need to upgrade Django or change the script to work with your version of Django. Upgrading is recommended, because 1.7 and older are now end of life, and do not receive security fixes. – Alasdair Mar 23 '16 at 16:23

Both django.setup() and django.apps were added in 1.7.

相關問題