Azure / azure-linux-extensions

Linux Virtual Machine Extensions for Azure
Apache License 2.0
301 stars 251 forks source link

fix imp python module for versions higher than 3.4 #1904

Closed NarineM closed 4 months ago

NarineM commented 4 months ago

Based on https://docs.python.org/3.6/library/imp.html imp has been deprecated starting python version 3.4

NarineM commented 4 months ago

Not able to repro on python 3.6, Can run import imp on python 3.4 as well as with python 3.6. Putting this change on hold (not needed). The module was replacedwith importlib but it is still present in 3.4/3.6. We ll need to revisit this code to take this fix in the future if it is removed from newer versions of python

matthew-rollitt commented 1 month ago

This isn't enough https://docs.python.org/dev/whatsnew/3.12.html#imp

on line 54 you also need to do this

import importlib.util import importlib.machinery

def load_source(modname, filename): loader = importlib.machinery.SourceFileLoader(modname, filename) spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) module = importlib.util.module_from_spec(spec)

The module is always executed and not cached in sys.modules.

# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module