Handle older versions of Python
import os
import sys
+import re
import logging
import importlib
import importlib.util
@@ -11,6 +12,14 @@ class ServicePackageError(ImportError):
pass
+try:
+ if ModuleNotFoundError:
+ pass
+except Exception:
+ class ModuleNotFoundError(ImportError):
+ pass
+
+
class ServiceImporter(object):
@classmethod
def import_service_file(cls, file_name: str) -> ModuleType:
@@ -53,6 +62,11 @@ class ServiceImporter(object):
logging.getLogger('import').warning('Invalid service package/parent name, may conflict with Python internals: "{}" - change parent folder name'.format(file_path.rsplit('/', 2)[1]))
raise ServicePackageError from e
raise e
+ except SystemError as e:
+ if re.match(r"^Parent module '.*' not loaded, cannot perform relative import$", str(e)):
+ logging.getLogger('import').warning('Invalid service package/parent name, may conflict with Python internals: "{}" - change parent folder name'.format(file_path.rsplit('/', 2)[1]))
+ raise ServicePackageError from e
+ raise e
except (ImportError, ModuleNotFoundError) as e: # noqa
if file_name.endswith('.py.py'):
return cls.import_service_file(file_name[:-3])
