Home » Django » Cannot import name _uuid_generate_random in heroku django

Cannot import name _uuid_generate_random in heroku django

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am working on a project which scans user gmail inbox and provides a report. I have deployed it in heroku with following specs:

Language: Python 2.7

Framework: Django 1.8

Task scheduler: Celery (Rabbitmq-bigwig for broker url)

Now when heroku execute it the celery is not giving me the output. On Heroku push its showing Collectstatic configuration error. I have tried using whitenoise package

Also tried executing: heroku run python manage.py collectstatic –dry-run –noinput
Still getting the same error.

$ heroku run python manage.py collectstatic –noinput gave the following
details of the error.

File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 303, in execute
settings.INSTALLED_APPS
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/app/.heroku/python/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/app/salesblocker/__init__.py", line 5, in <module>
from .celery import app as celery_app 
File "/app/salesblocker/celery.py", line 5, in <module>
from celery import Celery
File "/app/.heroku/python/lib/python2.7/site-packages/celery/__init__.py", line 131, in <module>
from celery import five  # noqa
File "/app/.heroku/python/lib/python2.7/site-packages/celery/five.py", line 153, in <module>
from kombu.utils.compat import OrderedDict  # noqa
File "/app/.heroku/python/lib/python2.7/site-packages/kombu/utils/__init__.py", line 19, in <module>
from uuid import UUID, uuid4 as _uuid4, _uuid_generate_random
ImportError: cannot import name _uuid_generate_random

I have also tried to rollback heroku commit to previous working commit and cloned that code but on the next commit(changes:removed a media image from the media folder) its showing the same error again.

Thanks in advance

Answers:

You are coming across this issue, which affects Python 2.7.11 (Kombu is required by Celery).

The issue is fixed in Kombu 3.0.30.

Questions:
Answers:

While upgrading kombu is the ideal option, if you are stuck with older dependencies that don’t allow for this, placing this at the top of my settings.py worked for me:

import uuid
uuid._uuid_generate_random = None

This works because _uuid_generate_random was removed here, and this simply restores the default value. This hack seems reasonable as Kombu only checks this to work around a bug resolved in 2007, and if you need this fix because of a recent Python update, you inherently aren’t affected 🙂

Questions:
Answers:

Yes, the issue Alasdair mentioned was responsible for the error. I solved the problem in my project by following this workflow to keep only the essential requirements-to-freeze.txt where I list Celery, but not its dependencies like Kombu.

Then, it’s enough to upgrade the essential packages and then re-freeze the full list of dependencies with the working Kombu version.

pip install --upgrade -r requirements-to-freeze.txt
pip freeze > requirements.txt

And test things to make sure the upgrade didn’t break something else 😉