Permission Error in importlib.machinery.SourceFileLoader().exec_module

Viewed 204

I'm running this code through a jupyter notebook from a pip installed library on windows 10:

import importlib.machinery
import importlib.util
import tempfile
import functools

def eval_code(code):
    r"""
    save code in a temporary file and import it as a module
    """
    with tempfile.NamedTemporaryFile() as new_file:
        new_file.write(bytes(code, 'ascii'))
        new_file.flush()
        loader = importlib.machinery.SourceFileLoader('main', new_file.name)
        spec = importlib.util.spec_from_loader(loader.name, loader)
        mod = importlib.util.module_from_spec(spec)
        loader.exec_module(mod)
    return mod

And it fails at loader.exec_module with a PermissionError: [Errno 13] Permission denied.

I don't understand the point. Like I was able to create the temporary file but not to load it as a module.

I'm also running as Admin, with every rights on the Temp folder.

0 Answers
Related