importlib.machinery.SourceFileLoader().exec_module 中的权限错误

问题描述

我正在 Windows 10 上通过 pip 安装库中的 jupyter notebook 运行此代码

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

它在 loader.exec_module 处失败,带有 PermissionError: [Errno 13] Permission denied

我不明白这一点。就像我能够创建临时文件但不能将其作为模块加载。

我也以管理员身份运行,对 Temp 文件夹拥有所有权限。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)