ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置 - Scraper
Posted
技术标签:
【中文标题】ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置 - Scraper【英文标题】:ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured - Scraper 【发布时间】:2019-12-02 23:25:34 【问题描述】:当我尝试在 Django 中运行我的爬虫时,我收到以下错误。
当我尝试使用内置的 Django 模型将抓取的数据放入数据库时发生这种情况。
回溯和错误:
/usr/bin/python3.6 /home/xxxxxx/Desktop/project/teo/movierater/scrap.py
Traceback (most recent call last):
File "/home/xxxxxx/Desktop/project/teo/movierater/scrap.py", line 7, in <module>
from teo.movierater.api.models import *
File "/home/xxxxxx/Desktop/project/teo/movierater/api/models.py", line 3, in <module>
class Author(models.Model):
File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 64, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
我把它放在bin/activate:
export DJANGO_SETTINGS_MODULE=mysite.settings
这里是来自 scraper.py 的代码 sn-p:
import django
import os
import requests
from bs4 import BeautifulSoup as bs
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from teo.movierater.api.models import *
os.environ['DJANGO_SETTINGS_MODULE'] = 'movierater.settings'
django.setup()
当我尝试在 django.setup() 下导入模型时:
import django
import os
import requests
from bs4 import BeautifulSoup as bs
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
os.environ['DJANGO_SETTINGS_MODULE'] = 'movierater.settings'
django.setup()
from teo.movierater.api.models import *
错误:
ModuleNotFoundError: No module named 'movierater'
我的项目结构和设置:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'movierater.api',
]
wsgi.py 文件:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'movierater.settings')
application = get_wsgi_application()
【问题讨论】:
你使用的是什么服务器? 在django.setup()
之后导入模型
@waynetech 我做到了,现在我得到了 - ModuleNotFoundError: No module named 'movierater'
@RamyMohamed 我只使用大象sql.com
在django.setup()
之后然后from api.models import *
?
【参考方案1】:
我可以在这里看到几个可能的问题。
使用movierater.settings
作为DJANGO_SETTINGS_MODULE
值,即export DJANGO_SETTINGS_MODULE=movierater.settings
movierater
的位置必须在您的PYTHONPATH
(ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings) 上
你是如何启动你的应用程序的,你试过python manage.py shell
吗?
如果你想以脚本的形式启动爬虫(例如python manage.py scraper
),那么最好的方法是编写自定义命令;看看use django: from "python manage.py shell" to python script
【讨论】:
我应该在哪里附加:export DJANGO_SETTINGS_MODULE=movierater.settings 它是一个环境变量,在你的shell中运行export DJANGO_SETTINGS_MODULE=movierater.settings
(digitalocean.com/community/tutorials/…)
此命令 export DJANGO_SETTINGS_MODULE=movierater.settings 无效。它会在 shell 中产生语法错误。以上是关于ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置 - Scraper的主要内容,如果未能解决你的问题,请参考以下文章
django.core.exceptions.ImproperlyConfigured:请求设置 USE_I18N,但未配置设置
django.core.exceptions.ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置。请帮帮我
django.core.exceptions.ImproperlyConfigured 关于设置 TEMPLATES
尝试设置 Django 时出现 ImproperlyConfigured("settings.DATABASES 配置不正确。") 错误
django.core.exceptions.ImproperlyConfigured:设置 SECRET_KEY 环境变量
django.core.exceptions.ImproperlyConfigured:constance.backends.database 应用程序未正确安装