Django 1.9 ImportError for import_module

拈花ヽ惹草 提交于 2019-12-17 11:18:27

问题


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'

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/32761566/django-1-9-importerror-for-import-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!