配置不正确:请求的设置为INSTALLED_APPS,但未配置设置-抓取工具 [英] ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured - Scraper
问题描述
当我尝试在Django中运行我的scraper时,出现以下错误.
When I try to run my scraper in Django I get the following error.
当我尝试使用内置的Django模型将抓取的数据放入数据库中时,就会发生这种情况.
This happened when I tried to put the scraped data into the database using the built-in Django models.
回溯和错误:
/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的代码段:
Here is the code snippet from scraper.py:
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()下面导入模型时:
When I try import models below 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文件:
wsgi.py file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'movierater.settings')
application = get_wsgi_application()
推荐答案
我在这里可以看到几个可能的问题.
I can see a couple of possible issues here.
-
使用
movierater.settings
作为DJANGO_SETTINGS_MODULE
值,即export DJANGO_SETTINGS_MODULE = movierater.settings
- please note that
movierater
's location must be on yourPYTHONPATH
(ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings)
如何启动您的应用,您是否尝试过 python manage.py shell
?
how do you start your app, did you try python manage.py shell
?
如果您想以脚本启动刮板(例如 python manage.py scraper
),那么最好的方法是编写自定义命令;看看使用django:来自"python manage.py shell"到python脚本
if you you want to start your scraper as a script (e.g. python manage.py scraper
) then the best way to do it is to write a custom command; take a look at use django: from "python manage.py shell" to python script
这篇关于配置不正确:请求的设置为INSTALLED_APPS,但未配置设置-抓取工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!