似乎他们取消了在Python 3中通过删除execfile()快速加载脚本的所有简单方法

我是否错过了一个明显的选择?


当前回答

注意,如果使用PEP-263编码声明,上述模式将失败 不是ASCII或utf-8。您需要找到数据的编码,并对其进行编码 在将其交给exec()之前。

class python3Execfile(object):
    def _get_file_encoding(self, filename):
        with open(filename, 'rb') as fp:
            try:
                return tokenize.detect_encoding(fp.readline)[0]
            except SyntaxError:
                return "utf-8"

    def my_execfile(filename):
        globals['__file__'] = filename
        with open(filename, 'r', encoding=self._get_file_encoding(filename)) as fp:
            contents = fp.read()
        if not contents.endswith("\n"):
            # http://bugs.python.org/issue10204
            contents += "\n"
        exec(contents, globals, globals)

其他回答

你可以写自己的函数:

def xfile(afile, globalz=None, localz=None):
    with open(afile, "r") as fh:
        exec(fh.read(), globalz, localz)

如果你真的需要…

如果可以,请避免使用exec()。对于大多数应用程序,使用Python的导入系统更简洁。

这个函数使用内置的importlib来执行一个文件作为一个实际的模块:

from importlib import util

def load_file_as_module(name, location):
    spec = util.spec_from_file_location(name, location)
    module = util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module

使用的例子

让我们有一个文件foo.py:

def hello():
    return 'hi from module!'
print('imported from', __file__, 'as', __name__)

并将其作为常规模块导入:

>>> mod = load_file_as_module('mymodule', './foo.py')
imported from /tmp/foo.py as mymodule
>>> mod.hello()
hi from module!
>>> type(mod)
<class 'module'>

优势

这种方法不会污染名称空间或破坏$PATH,而exec()则直接在当前函数的上下文中运行代码,可能会导致名称冲突。同样,模块属性如__file__和__name__将被正确设置,代码位置将被保留。因此,如果你附加了一个调试器,或者模块引发了一个异常,你将得到可用的回溯。

注意,与静态导入的一个微小区别是,每次运行load_file_as_module()时,模块都会被导入(执行),而不是像import关键字那样只导入一次。

正如python-dev邮件列表最近建议的那样,runpy模块可能是一个可行的替代方案。引用这条信息:

https://docs.python.org/3/library/runpy.html#runpy.run_path 进口runpy File_globals = runpy.run_path("file.py")

execfile有一些细微的区别:

Run_path总是创建一个新的名称空间。它将代码作为模块执行,因此全局变量和局部变量之间没有区别(这就是为什么只有init_globals参数)。返回全局变量。 在当前命名空间或给定命名空间中执行的Execfile。局部变量和全局变量的语义与类定义中的局部变量和全局变量相似。 Run_path不仅可以执行文件,还可以执行eggs和目录(详细信息请参阅其文档)。

如果你想要加载的脚本和你运行的脚本在同一个目录中,也许“import”就可以了?

如果你需要动态导入代码,内置函数__ import__和模块imp值得一看。

>>> import sys
>>> sys.path = ['/path/to/script'] + sys.path
>>> __import__('test')
<module 'test' from '/path/to/script/test.pyc'>
>>> __import__('test').run()
'Hello world!'

test.py:

def run():
        return "Hello world!"

如果您使用的是Python 3.1或更高版本,还应该看一看importlib。

注意,如果使用PEP-263编码声明,上述模式将失败 不是ASCII或utf-8。您需要找到数据的编码,并对其进行编码 在将其交给exec()之前。

class python3Execfile(object):
    def _get_file_encoding(self, filename):
        with open(filename, 'rb') as fp:
            try:
                return tokenize.detect_encoding(fp.readline)[0]
            except SyntaxError:
                return "utf-8"

    def my_execfile(filename):
        globals['__file__'] = filename
        with open(filename, 'r', encoding=self._get_file_encoding(filename)) as fp:
            contents = fp.read()
        if not contents.endswith("\n"):
            # http://bugs.python.org/issue10204
            contents += "\n"
        exec(contents, globals, globals)