Scrapy project can't find django.core.management

半世苍凉 提交于 2019-12-29 08:09:05

问题


I'm trying to follow the method here to 'Scrapy' data from the web and simultaneously save that data directly to my Django database using Scrapy's item pipeline.

However, when I try to run scrapy crawl spidername, I'm getting the error:

ImportError: No module named django.core.management

At first I thought it was because my Scrapy project was outside of my Django project folder, but even after I moved the whole project into my Django project folder I kept getting the same error. If I open a python shell inside the Scrapy project folder in its new location (inside my Django project folder), import django.core.management works fine. So what's going on?

EDIT: Some additional info: I'm doing this on a Webfaction server, and the path to my Django project is /home/gchorn/webapps/django_app/django_project. I'm using Django version 1.4.1, Scrapy 0.16 and Python2.7. The layout of the Django+Scrapy project is as follows:

django_project/
    __init__.py
    manage.py
    settings.py
    urls.py
    myproject #folder containing wsgi.py
    app1
    app2
    app3
    templates
    ScrapyProject/
        scrapy.cfg
        ScrapyProject/
            __init__.py
            items.py
            pipelines.py
            settings.py
            spiders/
                __init__.py
                my_spider.py

回答1:


Try setting this in your Spider's settings.py:

import os
import sys

sys.path.append('/home/gchorn/webapps/django_app')

os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'

Then you can import your model classes like:

from django_project.app1.models import some_model


来源:https://stackoverflow.com/questions/14686223/scrapy-project-cant-find-django-core-management

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!