Python 3 中 execfile 的替代品?

发布于 2024-11-16 01:30:49 字数 748 浏览 3 评论 0原文

Python 2 有内置函数 execfile,它是在 Python 3.0 中被删除。 这个问题讨论Python 3.0的替代方案,但有些已进行相当大的更改< a href="http://docs.python.org/py3k/library/importlib.html" rel="noreferrer">自 Python 3.0 起。

Python 3.2 的 execfile 的最佳替代品是什么和未来的 Python 3.x 版本

Python 2 had the builtin function execfile, which was removed in Python 3.0. This question discusses alternatives for Python 3.0, but some considerable changes have been made since Python 3.0.

What is the best alternative to execfile for Python 3.2, and future Python 3.x versions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱情眠于流年 2024-11-23 01:30:49

2to3 脚本替换

execfile(filename, globals, locals)

exec(compile(open(filename, "rb").read(), filename, 'exec'), globals, locals)

这似乎是官方推荐。您可能需要使用 with 块来确保文件再次立即关闭:

with open(filename, "rb") as source_file:
    code = compile(source_file.read(), filename, "exec")
exec(code, globals, locals)

您可以省略 globalslocals 参数来执行文件,或使用 exec(code, {}) 使用新的临时字典作为全局和本地字典,从而有效地在新的临时范围中执行文件。

The 2to3 script replaces

execfile(filename, globals, locals)

by

exec(compile(open(filename, "rb").read(), filename, 'exec'), globals, locals)

This seems to be the official recommendation. You may want to use a with block to ensure that the file is promptly closed again:

with open(filename, "rb") as source_file:
    code = compile(source_file.read(), filename, "exec")
exec(code, globals, locals)

You can omit the globals and locals arguments to execute the file in the current scope, or use exec(code, {}) to use a new temporary dictionary as both the globals and locals dictionary, effectively executing the file in a new temporary scope.

莫言歌 2024-11-23 01:30:49
execfile(filename)

可以替换为

exec(open(filename).read())

适用于所有版本的 Python

较新版本的 Python 会警告你没有关闭该文件,因此如果你想摆脱该警告,你可以这样做:

with open(filename) as infile:
    exec(infile.read())

但实际上,如果你关心关闭文件时,您应该足够小心,不要首先使用 exec 。

execfile(filename)

can be replaced with

exec(open(filename).read())

which works in all versions of Python

Newer versions of Python will warn you that you didn't close that file, so then you can do this is you want to get rid of that warning:

with open(filename) as infile:
    exec(infile.read())

But really, if you care about closing files, you should care enough to not use exec in the first place.

无需解释 2024-11-23 01:30:49

在Python3.x中,这是我能想到的最接近直接执行文件的方法,它与运行python /path/to/somefile.py相匹配。

注意:

  • 使用二进制读取来避免编码问题
  • Garenteed 关闭文件(Python3.x对此发出警告)
  • 定义__main__,一些脚本依赖于此来检查它们是否正在加载作为模块或不作为例如。 if __name__ == "__main__"
  • 设置 __file__ 对于异常消息来说更好,并且某些脚本使用 __file__ 来获取其他文件相对于它们的路径。
def exec_file(filepath):
    global_namespace = {
        "__file__": filepath,
        "__name__": "__main__",
    }
    with open(filepath, 'rb') as file:
        exec(compile(file.read(), filepath, 'exec'), global_namespace)

# Execute the file.
exec_file("/path/to/somefile.py")

In Python3.x this is the closest thing I could come up with to executing a file directly, that matches running python /path/to/somefile.py.

Notes:

  • Uses binary reading to avoid encoding issues
  • Garenteed to close the file (Python3.x warns about this)
  • defines __main__, some scripts depend on this to check if they are loading as a module or not for eg. if __name__ == "__main__"
  • setting __file__ is nicer for exception messages and some scripts use __file__ to get the paths of other files relative to them.
def exec_file(filepath):
    global_namespace = {
        "__file__": filepath,
        "__name__": "__main__",
    }
    with open(filepath, 'rb') as file:
        exec(compile(file.read(), filepath, 'exec'), global_namespace)

# Execute the file.
exec_file("/path/to/somefile.py")
作业与我同在 2024-11-23 01:30:49

标准 runpy.run_path 是一种替代方案。

Standard runpy.run_path is an alternative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文