gpt4 book ai didi

python - Scrapy django 模型导入报错

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:02 26 4
gpt4 key购买 nike

我一直在尝试使用 django 和 djangoitem 设置一个 scrapy 蜘蛛。当我尝试运行我的蜘蛛时,item.py 总是无法导入 django 应用程序的模型。

我建立了一个新的 django 站点并按照 https://stackoverflow.com/a/19073347/3988392 中的说明进行操作而且我仍然无法从我的应用程序导入模型。我认为一切都按照该线程中所述进行设置(除了我将事物称为 testsite 而不是 mysite 等。)但是如果有人认为它可能有帮助,我会发布代码。

运行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

最佳答案

我想你错过了将 pythonpath 写入你的 django 项目。您添加的是应用程序,而不是项目。

在 testbot/settings.py 添加:

# 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'

关于python - Scrapy django 模型导入报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25561467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com