From cfe311be0f9c7079d31afb94bdd6fa8c70cec211 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Behmo?= <regis@behmo.com>
Date: Thu, 30 Apr 2020 23:41:40 +0200
Subject: [PATCH] Quiet deprecation warnings from imp module

The "imp" module is deprecated and should be replaced by "importlib". As
a consequence, loading the django settings used to raise deprecation
warnings:

    DeprecationWarning: the imp module is deprecated in favour of
    importlib; see the module's documentation for alternative uses

It should be noted that python 3.5.1 ships with an older release of
distutils which still relies on the imp module. Thus, users of python
3.5.1 (for instance: edx.org developers) will continue to see the
deprecation warning for some time, despite this patch. We suggest
upgrading to python 3.5.9.

This addresses part of CRI-196.
---
 cms/envs/common.py | 8 +++-----
 lms/envs/common.py | 8 +++-----
 2 files changed, 6 insertions(+), 10 deletions(-)

diff --git a/cms/envs/common.py b/cms/envs/common.py
index 206182b89e9..99d7a3102c3 100644
--- a/cms/envs/common.py
+++ b/cms/envs/common.py
@@ -40,7 +40,7 @@ When refering to XBlocks, we use the entry-point name. For example,
 
 # pylint: disable=unused-import, useless-suppression, wrong-import-order, wrong-import-position
 
-import imp
+import importlib.util
 import os
 import sys
 from datetime import timedelta
@@ -1623,10 +1623,8 @@ OPTIONAL_APPS = (
 for app_name, insert_before in OPTIONAL_APPS:
     # First attempt to only find the module rather than actually importing it,
     # to avoid circular references - only try to import if it can't be found
-    # by find_module, which doesn't work with import hooks
-    try:
-        imp.find_module(app_name)
-    except ImportError:
+    # by find_spec, which doesn't work with import hooks
+    if importlib.util.find_spec(app_name) is None:
         try:
             __import__(app_name)
         except ImportError:
diff --git a/lms/envs/common.py b/lms/envs/common.py
index 4cfa792cdd6..220a988a223 100644
--- a/lms/envs/common.py
+++ b/lms/envs/common.py
@@ -29,7 +29,7 @@ Longer TODO:
 # pylint: disable=invalid-name, wrong-import-position
 
 
-import imp
+import importlib.util
 import sys
 import os
 
@@ -3181,10 +3181,8 @@ OPTIONAL_APPS = [
 for app_name, insert_before in OPTIONAL_APPS:
     # First attempt to only find the module rather than actually importing it,
     # to avoid circular references - only try to import if it can't be found
-    # by find_module, which doesn't work with import hooks
-    try:
-        imp.find_module(app_name)
-    except ImportError:
+    # by find_spec, which doesn't work with import hooks
+    if importlib.util.find_spec(app_name) is None:
         try:
             __import__(app_name)
         except ImportError:
-- 
GitLab