python - Haystack/Whoosh 索引生成错误

标签 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)

最佳答案

我刚刚遇到了具有完全不同堆栈的相同 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 的处理方式不同。

关于python - Haystack/Whoosh 索引生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1971356/

相关文章:

c# - C# 中的列表切片

django - 如何部署连接到 Django RESTful API 的静态网站?

python - django-haystack elasticsearch 作为后端和搜索引擎

django 干草堆 : Better ways of creating search indexes for models having foreign key and many-to-many fields

python - 使用Django Haystack仅匹配查询中的某些单词

python - 如何将 Python 库安装和更新到 SQL ML Server 实例中?

Python 从 panda 列转换为列表?

python - 为什么我需要 Selenium 中的 ChromeDriver?

python - 如何在 Django 1.9 中扩展用户模型?

python - 如何将 formbuilder 制作的表单添加到 Wagtail 中的每个页面?