aws / aws-lambda-python-runtime-interface-client

Apache License 2.0
263 stars 75 forks source link

Use importlib #28

Closed rmanganiello closed 3 years ago

rmanganiello commented 3 years ago

Problem

Remove imp in favor of importlib

imp module is deprecated since Python 3.3

The previous implementation had some issues while trying to load python modules ( execute __init__.py files) when you specify a handler in a directory.

By using importlib these issues go away and the implementation is way easier.

Description of changes

Source

https://docs.python.org/3/library/imp.html#imp.find_module

Deprecated since version 3.3: Use importlib.util.find_spec() instead unless Python 3.3 compatibility is required, in which case use importlib.find_loader(). For example usage of the former case, see the Examples section of the importlib documentation.

https://docs.python.org/3/library/imp.html#imp.load_module

Deprecated since version 3.3: If previously used in conjunction with imp.find_module() then consider using importlib.import_module(), otherwise use the loader returned by the replacement you chose for imp.find_module(). If you called imp.load_module() and related functions directly with file path arguments then use a combination of importlib.util.spec_from_file_location() and importlib.util.module_from_spec(). See the Examples section of the importlib documentation for details of the various approaches.

https://docs.python.org/3/library/importlib.html#importlib.invalidate_caches

If you are dynamically importing a module that was created since the interpreter began execution (e.g., created a Python source file), you may need to call invalidate_caches() in order for the new module to be noticed by the import system.

adamantike commented 3 years ago

@carlzogh @edhzsz @krk Gentle ping, based on other merge request approvals!