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.