python - 属性错误: 'module' object has no attribute 'setup' ; django setup working in one project but not other

标签 python django django-settings

#!/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/

相关文章:

python - 迭代 DataFrame 时设置值

python - 如何在 django 模型中以 (mm-dd-yyyy) 格式创建日期字段

django - 从 django settings.py 包含 local_settings.py 时如何修改 DATABASES 变量

python - 将日期序列分配给 pandas groupby 组

python - 如何在 Matplotlib 中将颜色设置为矩形?

python - DateTimeField 的 django inspectdb 选项

python - 当前在 Django 模板中获取主页 URL(域)的方法?

python - Django:在运行服务器之后立即运行脚本

python - 使用带有 django 搜索引擎的 soundex 功能

mysql - 将小型静态表合并到数据库中是一种好习惯吗?