2

Firstly, As Python3 has removed execfile, it's necessary to write you're own, thats fine, however I would like to be use pyc cache on future executions.

Importing a module in Python can auto-generate a pyc file, which will be used to avoid parsing the file when a new Python process needs to import the module again.

Is there a way to make use of the pyc caching mechanism, which run the code directly (so it doesn't need to be in sys.path or get its self added to sys.modules).

eg:

def my_execfile(filepath):
    globals = {'__name__': '__main__', '__file__': filepath}
    locals = {}
    with open(filepath, 'rb') as file:
        exec(compile(file.read(), filepath, 'exec'), globals, locals)

Is there a way to have something approximating this function, that runs a file - that will create/use a pyc when possible?

Or do I need to write my own pyc cache checker which handles the details of checking the path, comparing file time stamps, writes out the bytecode .. etc.

ideasman42
  • 42,413
  • 44
  • 197
  • 320

1 Answers1

0

This can be done using importlib, even though it's using import logic, it works like regular file execution.

def execfile_cached(filepath, mod=None):
    # module name isn't used or added to 'sys.modules'.
    # passing in 'mod' allows re-execution
    # without having to reload.

    import importlib.util
    mod_spec = importlib.util.spec_from_file_location("__main__", filepath)
    if mod is None:
        mod = importlib.util.module_from_spec(mod_spec)
    mod_spec.loader.exec_module(mod)
    return mod
ideasman42
  • 42,413
  • 44
  • 197
  • 320