#!/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.
关于python - 属性错误: 'module' object has no attribute 'setup' ; django setup working in one project but not other,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36183241/