Python import_module 的 Django 1.9 导入错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32761566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django 1.9 ImportError for import_module
提问by Matt
When trying to run either runserver
or shell
using manage.py
I get an ImportError
exception. I'm using Django 1.9.
尝试运行runserver
或shell
使用时manage.py
出现ImportError
异常。我正在使用 Django 1.9。
ImportError: No module named 'django.utils.importlib'
采纳答案by knbk
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.
django.utils.importlib
是仍然支持 Python 2.6 时的兼容性库。它从 Django 1.7 开始就已经过时了,Django 1.7 放弃了对 Python 2.6 的支持,并在每个弃用周期中在 1.9 中被删除。
Use Python's import_module
function instead:
改用 Python 的import_module
函数:
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 notbecause module_loading
in any way defines the actual function.
你可以从导入它的原因django.utils.module_loading
即importlib.import_module
是,模块进口,这是不是因为module_loading
以任何方式限定的实际功能。
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.
由于django.utils.module_loading.import_module
它不是公共 API 的一部分,因此如果不再使用它,可以随时将其删除 - 即使是在次要版本升级中。
回答by Matt
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