Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 1.9 removed get_cache from django.core.cache #149

Closed
grumpa opened this issue Dec 18, 2015 · 9 comments
Closed

Django 1.9 removed get_cache from django.core.cache #149

grumpa opened this issue Dec 18, 2015 · 9 comments

Comments

@grumpa
Copy link

grumpa commented Dec 18, 2015

Rosetta 0.7.8, Django 1.9. Using memcached backend. Error:

rosetta/poutil.py
from django.core.cache import get_cache
ImportError: cannot import name 'get_cache'

Django 1.9 release notes: django.core.cache.get_cache is removed

@mbi
Copy link
Owner

mbi commented Dec 18, 2015

Not sure how to trigger that. That code branch is only called for backward compatibility, if the first import in https://github.com/mbi/django-rosetta/blob/develop/rosetta/storage.py#L10 fails. It won't ever be called on Django 1.9, or am I missing something?

@grumpa
Copy link
Author

grumpa commented Dec 18, 2015

It's me who missed something. I had problems with behavior with web interface so i tried to switch to different cache then the default LocMem. I put MemCached to setting and the problem has begun.
I think now I have improperly configured MemCached. So i apologize.

@mbi
Copy link
Owner

mbi commented Dec 18, 2015

Sweet, thank you.

@mbi mbi closed this as completed Dec 18, 2015
@saschwarz
Copy link

saschwarz commented May 14, 2016

I'm also getting this error in django 1.9 with rosetta 0.7.11 when I use Memcached cache backend. Here's the full traceback:

Unhandled exception in thread started by <function wrapper at 0x1113932a8>
Traceback (most recent call last):
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
    self.check(display_num_errors=True)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/saschwarz/dev/agilitycourses/urls.py", line 44, in <module>
    url(r'^rosetta/', include('rosetta.urls')),
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/rosetta/urls.py", line 2, in <module>
    from .views import (home, list_languages, download_file, lang_sel, translate_text, ref_sel)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/rosetta/views.py", line 18, in <module>
    from rosetta.poutil import find_pos, pagination_range, timestamp_with_timezone
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/rosetta/poutil.py", line 16, in <module>
    from django.core.cache import get_cache
ImportError: cannot import name get_cache

If I remove rosetta from the INSTALLED_APPS I can see is how I've configured CACHES:

$ python manage.py shell --settings=dev_settings
Python 2.7.8 (default, Oct 19 2014, 16:06:28) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.conf import settings
>>> settings.CACHES
{'default': {'LOCATION': '127.0.0.1:11211', 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache'}}
>>> 

If I remove my CACHES definition and add rosetta back into INSTALLED_APPS django starts and runs using the default cache:

$ python manage.py shell --settings=dev_settings
Python 2.7.8 (default, Oct 19 2014, 16:06:28) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.conf import settings
>>> settings.CACHES
{'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}}
>>> 

I've running this configuration in production using django 1.7 and rosetta 0.7.4 and don't see this error.
In dev I'm working on upgrading to django 1.9 and rosetta 0.7.11 and if I downgrade rosetta to 0.7.4 I also see the traceback at start up. So it appears to be introduced by the API change in django 1.9

Do I have my CACHES misconfigured? Thanks!

@mbi
Copy link
Owner

mbi commented May 14, 2016

Hi @saschwarz. Have you overridden ROSETTA_CACHE_NAME?

I.e. what is the output of this?

from rosetta.conf import settings as rosetta_settings
print(rosetta_settings.ROSETTA_CACHE_NAME)

It should match one of your cache backends, probably default.

@saschwarz
Copy link

saschwarz commented May 14, 2016

Thanks for the fast reply! I haven't overridden it:

>>> from rosetta.conf import settings as rosetta_settings
>>> print(rosetta_settings.ROSETTA_CACHE_NAME)
default

@mbi
Copy link
Owner

mbi commented May 14, 2016

Okay interesting, does any of these lines throw an error?

from rosetta.conf import settings as rosetta_settings
from django.core.cache import caches
cache = caches[rosetta_settings.ROSETTA_CACHE_NAME]
print(cache)

@saschwarz
Copy link

saschwarz commented May 14, 2016

That gave the answer:

 $ python manage.py shell --settings=dev_settings
Python 2.7.8 (default, Oct 19 2014, 16:06:28) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from rosetta.conf import settings as rosetta_settings
>>> from django.core.cache import caches
>>> cache = caches[rosetta_settings.ROSETTA_CACHE_NAME]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/core/cache/__init__.py", line 80, in __getitem__
    cache = _create_cache(alias)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/core/cache/__init__.py", line 55, in _create_cache
    return backend_cls(location, params)
  File "/Users/saschwarz/.virtualenvs/django19/lib/python2.7/site-packages/django/core/cache/backends/memcached.py", line 159, in __init__
    import memcache
ImportError: No module named memcache

In my new Django 1.9 venv I hadn't installed memcache! I hadn't noticed it because the new rosetta code checked that I had an incompatible cache backend and when I enabled my production backed in dev the ImportError was being caught/handled by: https://github.com/mbi/django-rosetta/blob/develop/rosetta/poutil.py#L12

I guess you could do something to not hide that type of traceback:

try:
    from django.core.cache import caches
    cache = caches[rosetta_settings.ROSETTA_CACHE_NAME]
except ImportError as e:
    try:
        from django.core.cache import get_cache
        cache = get_cache(rosetta_settings.ROSETTA_CACHE_NAME)
    except ImportError:
        if 'No module named' in e.message:
            raise e
        raise

But it could be brittle... or my situation is unusual... I could create a PR if you think this is valuable.

Sorry to have taken your time for my mistake. Thanks so much for all your help debugging this!

@mbi
Copy link
Owner

mbi commented May 14, 2016

In your case the missing memcached exception was swallowed by that ImportError handler, but actually we no longer need that, as Rosetta only supports Django 1.7+ now. I've removed it in c80f2db

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants