Reputation: 96
I've been trying to set up a scrapy spider using django and djangoitem. When I try running my spider, the item.py always fails to import the django app's model.
I set up a fresh django site and followed the instructions from https://stackoverflow.com/a/19073347/3988392 and I'm still having trouble importing models from my app. I think everything is set up as stated in that thread (except I called things testsite rather than mysite, etc..) But I'll post code if anyone thinks it could be helpful.
Traceback when running scrapy crawl example
:
Traceback (most recent call last):
File "/usr/bin/scrapy", line 9, in <module>
load_entry_point('Scrapy==0.24.4', 'console_scripts', 'scrapy')()
File "/usr/lib/pymodules/python2.7/scrapy/cmdline.py", line 143, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "/usr/lib/pymodules/python2.7/scrapy/cmdline.py", line 89, in _run_print_help
func(*a, **kw)
File "/usr/lib/pymodules/python2.7/scrapy/cmdline.py", line 150, in _run_command
cmd.run(args, opts)
File "/usr/lib/pymodules/python2.7/scrapy/commands/crawl.py", line 57, in run
crawler = self.crawler_process.create_crawler()
File "/usr/lib/pymodules/python2.7/scrapy/crawler.py", line 87, in create_crawler
self.crawlers[name] = Crawler(self.settings)
File "/usr/lib/pymodules/python2.7/scrapy/crawler.py", line 25, in __init__
self.spiders = spman_cls.from_crawler(self)
File "/usr/lib/pymodules/python2.7/scrapy/spidermanager.py", line 35, in from_crawler
sm = cls.from_settings(crawler.settings)
File "/usr/lib/pymodules/python2.7/scrapy/spidermanager.py", line 31, in from_settings
return cls(settings.getlist('SPIDER_MODULES'))
File "/usr/lib/pymodules/python2.7/scrapy/spidermanager.py", line 22, in __init__
for module in walk_modules(name):
File "/usr/lib/pymodules/python2.7/scrapy/utils/misc.py", line 68, in walk_modules
submod = import_module(fullpath)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/myUserName/test-env/testbot/testbot/spiders/example.py", line 3, in <module>
from testbot.items import UsefulModelItem
File "/home/myUserName/test-env/testbot/testbot/items.py", line 11, in <module>
from testapp.models import UsefulModel
ImportError: No module named testapp.models
├── testbot
│ ├── scrapy.cfg
│ └── testbot
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── items.py
│ ├── items.pyc
│ ├── pipelines.py
│ ├── settings.py
│ ├── settings.pyc
│ └── spiders
│ ├── example.py
│ ├── example.pyc
│ ├── __init__.py
│ └── __init__.pyc
└── testsite
├── manage.py
├── testapp
│ ├── admin.py
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ └── views.py
└── testsite
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.pyc
├── urls.py
└── wsgi.py
Upvotes: 2
Views: 843
Reputation: 533
import sys
import os
# Setting up django's project full path.
sys.path.append('/home/testsite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'testsite.settings'
Upvotes: 0
Reputation: 1025
I think you miss to write the pythonpath to your django project. You are adding the app, not the project.
in testbot/settings.py add:
# Setting up django's project full path.
import sys
sys.path.insert(0, '/home/TristanTrim/PycharmProjects/testsite')
# Setting up django's settings module name.
# This module is located at /home/rolando/projects/myweb/myweb/settings.py.
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'testsite.settings'
Upvotes: 2