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?
The
2to3script replacesby
This seems to be the official recommendation. You may want to use a
withblock to ensure that the file is promptly closed again:You can omit the
globalsandlocalsarguments to execute the file in the current scope, or useexec(code, {})to use a new temporary dictionary as both the globals and locals dictionary, effectively executing the file in a new temporary scope.