What is an alternative to execfile in Python 3?
-
22-07-2019 - |
Question
It seems they canceled in Python 3 all the easy way to quickly load a script by removing execfile()
Is there an obvious alternative I'm missing?
Solution
According to the documentation, instead of
execfile("./filename")
Use
exec(open("./filename").read())
See:
OTHER TIPS
You are just supposed to read the file and exec the code yourself. 2to3 current replaces
execfile("somefile.py", global_vars, local_vars)
as
with open("somefile.py") as f:
code = compile(f.read(), "somefile.py", 'exec')
exec(code, global_vars, local_vars)
(The compile call isn't strictly needed, but it associates the filename with the code object making debugging a little easier.)
See:
While exec(open("filename").read())
is often given as an alternative to execfile("filename")
, it misses important details that execfile
supported.
The following function for Python3.x is as close as I could get to having the same behavior as executing a file directly. That matches running python /path/to/somefile.py
.
def execfile(filepath, globals=None, locals=None):
if globals is None:
globals = {}
globals.update({
"__file__": filepath,
"__name__": "__main__",
})
with open(filepath, 'rb') as file:
exec(compile(file.read(), filepath, 'exec'), globals, locals)
# execute the file
execfile("/path/to/somefile.py")
Notes:
- Uses binary reading to avoid encoding issues
- Guaranteed 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. Takes optional globals & locals arguments, modifying them in-place as
execfile
does - so you can access any variables defined by reading back the variables after running.Unlike Python2's
execfile
this does not modify the current namespace by default. For that you have to explicitly pass inglobals()
&locals()
.
As suggested on the python-dev mailinglist recently, the runpy module might be a viable alternative. Quoting from that message:
https://docs.python.org/3/library/runpy.html#runpy.run_path
import runpy file_globals = runpy.run_path("file.py")
There are subtle differences to execfile
:
run_path
always creates a new namespace. It executes the code as a module, so there is no difference between globals and locals (which is why there is only ainit_globals
argument). The globals are returned.execfile
executed in the current namespace or the given namespace. The semantics oflocals
andglobals
, if given, were similar to locals and globals inside a class definition.run_path
can not only execute files, but also eggs and directories (refer to its documentation for details).
This one is better, since it takes the globals and locals from the caller:
import sys
def execfile(filename, globals=None, locals=None):
if globals is None:
globals = sys._getframe(1).f_globals
if locals is None:
locals = sys._getframe(1).f_locals
with open(filename, "r") as fh:
exec(fh.read()+"\n", globals, locals)
You could write your own function:
def xfile(afile, globalz=None, localz=None):
with open(afile, "r") as fh:
exec(fh.read(), globalz, localz)
If you really needed to...
If the script you want to load is in the same directory than the one you run, maybe "import" will do the job ?
If you need to dynamically import code the built-in function __ import__ and the module imp are worth looking at.
>>> 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!"
If you're using Python 3.1 or later, you should also take a look at importlib.
Here's what I had (file
is already assigned to the path to the file with the source code in both examples):
execfile(file)
Here's what I replaced it with:
exec(compile(open(file).read(), file, 'exec'))
My favorite part: the second version works just fine in both Python 2 and 3, meaning it's not necessary to add in version dependent logic.
Note that the above pattern will fail if you're using PEP-263 encoding declarations that aren't ascii or utf-8. You need to find the encoding of the data, and encode it correctly before handing it to 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)
Also, while not a pure Python solution, if you're using IPython (as you probably should anyway), you can do:
%run /path/to/filename.py
Which is equally easy.
I'm just a newbie here so maybe it's pure luck if I found this :
After trying to run a script from the interpreter prompt >>> with the command
execfile('filename.py')
for which I got a "NameError: name 'execfile' is not defined" I tried a very basic
import filename
it worked well :-)
I hope this can be helpful and thank you all for the great hints, examples and all those masterly commented pieces of code that are a great inspiration for newcomers !
I use Ubuntu 16.014 LTS x64. Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux