AttributeError: 'module' object has no attribute 'setup'; django setup working in one project but not other
AttributeError: 'module' object has no attribute 'setup'; django setup working in one project but not other
#!/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
尝试:
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
在manage.py中:
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webapi.server.project.settings")
因此我的 os.environ
设置与另一个项目中的格式相匹配。我 运行 这个脚本在其他 django 项目中以相同的方式工作并且它有效,但在我的 webapi 中没有。这些项目在 pythonpath 上。我该如何设置 Django?谢谢
您需要将 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.
#!/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
尝试:
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
在manage.py中:
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webapi.server.project.settings")
因此我的 os.environ
设置与另一个项目中的格式相匹配。我 运行 这个脚本在其他 django 项目中以相同的方式工作并且它有效,但在我的 webapi 中没有。这些项目在 pythonpath 上。我该如何设置 Django?谢谢
您需要将 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.