Haystack / Whoosh Index Generation Error

天涯浪子 提交于 2019-12-10 18:58:47

问题


I'm trying to setup haystack with whoosh backend. When i try to gen the index [or any index command for that matter] i receive:

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

if i completely remove my search_indexes.py i get the same error [so i'm guessing it can't find that file at all]

what might cause this error? it's set to autodiscover and i'm sure my app is installed because i'm currently using it.

Full traceback:

    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

and here is my search_indexes.py

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

site.register(myobject)

回答1:


It seems like there are two problems you're running into.

The first is the one that's generating that TypeError. It occurs while Haystack is searching through each app you have listed in INSTALLED_APPS for a search_indexes.py (since you're auto-registering). I'm not sure exactly what the problem is, but I'd start by doing a search through your project for from list and double-checking your code. I haven't encountered that exception before, but if that's happening in code you wrote you should post any relevant sections in your question

I believe the reason you get the same error with or without the search_indexes.py file is because it never reaches the point of trying to execute the code in that file.

That said, there should be more happening in that file (which is the second problem). You have to create an index class (that inherits from haystack.indexes.SearchIndex) and register that with the model. See this section of the documentation for instructions and an example.

I'd also ask this question in the django-haystack Google Group since the author and other users of haystack will see it there and they tend to be extremely helpful.




回答2:


I've just encountered the same TypeError message with a completely different stack.

A search on the whole error message brought up two results: this question, and the source code for Python's import.c. So after a little digging, I find that this particular error is caused when the __import__ builtin is passed an import name which isn't a string.

The important word there is string - ie. a str object. Anything else (eg. unicode) will be rejected with the error described here.

So the solution is: wherever you're passing a module/member name to something which will dynamically import it, make sure that it's a str and not a unicode.

Fails:

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

Work:

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

Of course, this is probably only relevant to Python 2.x, given that 3.x does strings/unicode differently.




回答3:


In my case, this occurred after I upgraded my django-tastypie to v0.10. As part of the Py3 porting effort, from __future__ import unicode_literals was added to the top of the migrations.

After commenting that line out in each of the tastypie migrations files, my migrations have run OK.

What I find puzzling is the fact that the tastypie migrations ran OK yesterday with the new tastypie version ( in a separate project that shares the same virtualenv ). That is a mystery for another day.




回答4:


I bumped into the same error last night, on a codebase that was working 5 minutes before without any modification of any kind. As far up as I went through my git repo, code that was working previously caused the same error. I cast my unicode value to string and it removed the problem but did not solve the underlying cause.

So I figured out that if:

  1. it did not come from my code
  2. it was raised in python function import
  3. neither my python nor code base had changed

the problem had to be in the bit code. I deleted every .pyc and .pyo file in my application. And the error was gone.

deleting the .py- files:

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


来源:https://stackoverflow.com/questions/1971356/haystack-whoosh-index-generation-error

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