干草堆/飞快指数生成错误

时间:2009-12-28 20:21:30

标签: python django django-haystack whoosh

我正在尝试使用whoosh后端设置haystack。当我尝试生成索引[或任何索引命令]时,我收到:

TypeError: Item in ``from list'' not a string

如果我完全删除了我的search_indexes.py,我会得到同样的错误[所以我猜它根本找不到该文件]

可能导致此错误的原因是什么?它设置为自动发现,我确定我的应用程序已安装,因为我正在使用它。

完整追溯:

    Traceback (most recent call last):
  File "./manage.py", line 17, in <module>
    execute_manager(settings)
  File "/Users/ghostrocket/Development/Redux/.dependencies/django/core/management/__init__.py", line 362, in execute_manager
    utility.execute()
  File "/Users/ghostrocket/Development/Redux/.dependencies/django/core/management/__init__.py", line 303, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/ghostrocket/Development/Redux/.dependencies/django/core/management/__init__.py", line 257, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/ghostrocket/Development/Redux/.dependencies/django/core/management/__init__.py", line 67, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/Users/ghostrocket/Development/Redux/.dependencies/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/ghostrocket/Development/Redux/.dependencies/haystack/__init__.py", line 124, in <module>
    handle_registrations()
  File "/Users/ghostrocket/Development/Redux/.dependencies/haystack/__init__.py", line 121, in handle_registrations
    search_sites_conf = __import__(settings.HAYSTACK_SITECONF)
  File "/Users/ghostrocket/Development/Redux/website/../website/search_sites.py", line 2, in <module>
    haystack.autodiscover()
  File "/Users/ghostrocket/Development/Redux/.dependencies/haystack/__init__.py", line 83, in autodiscover
    app_path = __import__(app, {}, {}, [app.split('.')[-1]]).__path__
TypeError: Item in ``from list'' not a string

这是我的search_indexes.py

from haystack import indexes
from haystack import site
from myproject.models import *

site.register(myobject)

4 个答案:

答案 0 :(得分:8)

我刚刚遇到了具有完全不同堆栈的相同TypeError消息。

搜索整个错误消息带来了两个结果:这个问题,以及Python的import.c的源代码。 因此,经过一番挖掘后,我发现当__import__内置函数传递了一个不是字符串的导入名称时,会导致此特定错误。

重要的一个词是 string - 即。一个str个对象。其他任何内容(例如unicode)将被拒绝,并带有此处描述的错误。

所以解决方案是:无论你将模块/成员名称传递给动态导入它的地方,都要确保它是str而不是unicode

失败:

__import__('mylib.foo', globals(), locals(), [u'bar'])

工作:

__import__('mylib.foo', globals(), locals(), ['bar'])
__import__(u'mylib.foo', globals(), locals(), ['bar'])

当然,这可能只与Python 2.x有关,因为3.x的字符串/ unicode不同。

答案 1 :(得分:2)

在我的情况下,这是在我将django-tastypie升级到v0.10之后发生的。作为Py3移植工作的一部分,from __future__ import unicode_literals被添加到迁移的顶部。

在每个tastypie迁移文件中注释掉该行后,我的迁移运行正常。

令我感到困惑的是,tastypie迁移昨天运行了新的tastypie版本(在一个共享相同virtualenv的单独项目中)。这是另一天的一个谜。

答案 2 :(得分:1)

您似乎遇到了两个问题。

第一个是生成TypeError的那个。当Haystack搜索您在INSTALLED_APPS中列出的每个应用搜索search_indexes.py(因为您正在自动注册)时,就会发生这种情况。我不确定问题究竟是什么,但我首先要搜索你的项目from list并仔细检查你的代码。我以前没有遇到过这个例外,但是如果您在编写的代码中发生这种情况,则应该在您的问题中发布任何相关部分

我相信你在使用或不使用search_indexes.py文件时得到相同错误的原因是因为它永远不会到达尝试执行该文件中的代码的程度。

那就是说,该文件应该发生更多(这是第二个问题)。您必须创建一个索引类(继承自haystack.indexes.SearchIndex)并将其注册到模型中。有关说明和示例,请参阅this section of the documentation

我也会在django-haystack Google Group中提出这个问题,因为作者和haystack的其他用户会在那里看到它并且它们往往非常有帮助。

答案 3 :(得分:1)

昨晚我在5分钟前工作的代码库没有任何修改的情况下碰到了同样的错误。 到目前为止,我通过我的git repo,以前工作的代码导致了同样的错误。 我将我的unicode值转换为字符串,它删除了问题,但没有解决根本原因。

所以我想出了如果:

  1. 它不是来自我的代码
  2. 它是在python函数 import
  3. 中引发的
  4. 我的python和代码库都没有改变
  5. 问题必须在位代码中。我删除了我的应用程序中的每个.pyc和.pyo文件。错误消失了。

    删除.py文件:

    find . -name "*.pyc" -exec rm -f {} \;
    find . -name "*.pyo" -exec rm -f {} \;