Django 1.9 ImportError for import_module
Asked Answered
C

2

38

When trying to run either runserver or shell using manage.py I get an ImportError exception. I'm using Django 1.9.

ImportError: No module named 'django.utils.importlib'
Centre answered 24/9, 2015 at 12:38 Comment(3)
Note that Django 1.9 has only just reached alpha status, and is not suitable for production use. Use 1.8 unless you have a very good reason.Harquebus
Thanks Daniel. I'm only running it in local development at the moment, it's more of a means to help the Django team track down issues that would hold back a stable release.Centre
Django 1.9 is out now.Rayraya
S
68

django.utils.importlib is a compatibility library for when Python 2.6 was still supported. It has been obsolete since Django 1.7, which dropped support for Python 2.6, and is removed in 1.9 per the deprecation cycle.

Use Python's import_module function instead:

from importlib import import_module

The reason you can import it from django.utils.module_loading is that importlib.import_module is imported in that module, it is not because module_loading in any way defines the actual function.

Since django.utils.module_loading.import_module is not part of the public API, it can be removed at any time if it is no longer used - even in a minor version upgrade.

Scrooge answered 24/9, 2015 at 14:12 Comment(0)
C
20

I solved this with the following:

try:
    # Django versions >= 1.9
    from django.utils.module_loading import import_module
except ImportError:
    # Django versions < 1.9
    from django.utils.importlib import import_module
Centre answered 24/9, 2015 at 12:38 Comment(5)
In which file can i write this code? I updated my django from 1.7 to 1.9. And stopped work.Daysidayspring
looks like a bug in endless_pagination or similarRayraya
Thank you. I installed django-request to monitor traffic. I made the change you suggested in plugins.py and traffic.py and all is well now.Sizeable
Hi I just want to ask while file should I write this code?Grous
a safer solution for meGoode

© 2022 - 2024 — McMap. All rights reserved.