Recommended practice for using import_module in django >= 1.8
Asked Answered
W

2

7

In one of my django tests, I use django.utils.importlib.import_module similarly to how it is used here.

On upgrading to django 1.8, I get a deprecation warning

test_views.py:20: RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
  from django.utils.importlib import import_module

Is the recommended practice now to use import_module from importlib in the standard library (which seems to work fine)? Or is the recommended practice to avoid using import_module entirely?

Weaver answered 28/4, 2015 at 23:56 Comment(0)
R
12

Based on the documentation (found by looking at the django deprecation timeline for 1.9 and following the link), your first suggestion seems to be correct: developers should use the standard importlib that is part of the standard library.

Remediless answered 29/4, 2015 at 0:2 Comment(0)
H
8

Yes, django.utils.importlib.import_module is effectively the same thing as importlib.import_module. As long as you're not targeting a version of Python before 2.7, it is okay to just use importlib.import_module.

Hemlock answered 29/4, 2015 at 0:2 Comment(1)
it has problems with Python3 version < 3.4, because Django tries to use find_spec but find_spec was added only in 3.4.Housewares

© 2022 - 2024 — McMap. All rights reserved.