Importerror: No module named memcache (Django project)
Asked Answered
C

3

25

In a Django project of mine, I run this command to run the project on localhost:

python manage.py runserver

It results in the error:

Importerror: No module named memcache

However, I've already fulfilled the requirement via: sudo apt-get install python-memcache

Peculiarly, if I go into the python shell outside my virtualevn and try import memcache, it works fine. However, inside my virtualenv, if I go into the python shell and try import memcache, I get the same import error listed above. What's going on?

Cassel answered 10/1, 2017 at 18:14 Comment(5)
You might have two versions installed, and your app doesn't run with the default one.Melisenda
Possible. Any suggestions on how do I start debugging this?Cassel
run python --version to check your default Python interpreter. Check the first line of your app main script (if you don't know where is it, just call which <app_name>Melisenda
You are using virtualenv to run this application?.. if so, just do pip install memcache from inside your venv and try to import/run it again.Melisenda
That worked. To be exact, I did pip install python-memcached (notice the d). If you write that as an answer, I'll accept it.Cassel
M
64

As you are using virtualenv you'd need to install this dependency from inside as you might have created the virtual environment before installed it as a system-wide library.

After activate your virtualenv type:

pip install python-memcached

This should solve it.

Melisenda answered 10/1, 2017 at 18:52 Comment(0)
W
3

Based on pymemcache documentation

Since version 3.2, Django has included a pymemcache-based cache backend. See its documentation. On older Django versions, you can use django-pymemcache.

So for Django 3.2+ use :

pip install pymemcache
Widow answered 13/3, 2022 at 16:21 Comment(0)
T
1

First run

pip install django-pylibmc

set you cache backend:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        'LOCATION': '127.0.0.1.11211',
    }
}
Tarkington answered 3/12, 2018 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.