question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Django 1.9 removed get_cache from django.core.cache

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mbicommented, 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 c80f2db4a3ec11bf8c5636db29ce0ceb278e4a7e

0reactions
saschwarzcommented, 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django's cache framework
Set BACKEND to django.core.cache.backends.memcached. ... Be careful with this; clear() will remove everything from the cache, not just the keys set by your ......
Read more >
Expire a view-cache in Django? - python - Stack Overflow
The cache_page decorator abstracts caching quite a bit for you and makes it hard to delete a certain cache object directly. You could...
Read more >
Source code for django.utils.cache
This module contains helper functions for controlling caching. It does so by managing the "Vary" header of responses.
Read more >
Caching in Django With Redis - Real Python
Personally, we like how easy it is to set up and use Redis for other purposes such as Redis Queue. Remove ads. Getting...
Read more >
Django - Caching - Tutorialspoint
Django comes with its own caching system that lets you save your dynamic pages, to avoid calculating them again when needed. The good...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found