execfile() was a Python 2 built-in that read a source file and ran its code in the current (or a given) namespace. It does not exist in Python 3, which removed it in favour of an explicit exec(compile(...)) call, runpy, or a normal import. If you are on Python 3.12 or newer, use runpy.run_path() for scripts and importlib for modules.
This article explains what execfile did, why it was removed, and which modern replacement fits each situation.
In Python 2, execfile(filename[, globals[, locals]]) opened the file, compiled it, and executed it in the namespace you supplied. Without arguments it ran in the caller's namespace, so every top-level name defined in the file appeared in your code.
Passing a dictionary kept the names out of your own scope:
Calling execfile in Python 3 raises NameError: name 'execfile' is not defined. The statement print result above is also Python 2 syntax; in Python 3 print is a function.
Python 3.0 (2008) dropped execfile because it was a thin wrapper over operations the language already provides: reading a file, compiling it, and calling exec. Keeping the steps explicit has real benefits:
compile(), so tracebacks point at the right file and line.The direct translation reads the file in binary mode, compiles it with the filename, and executes the code object. Binary mode lets Python honour a # -*- coding: ... -*- declaration or the default UTF-8 encoding.
The function returns the dictionary instead of leaking names into the caller. This matters because inside a function, exec cannot reliably create local variables; code that assigned names with exec and then read them as plain variables in the same function does not work in Python 3. Always read results back from an explicit dictionary.
If you really need the old behaviour at module level, pass globals():
Prefer a dedicated dictionary. Sharing globals() lets the executed file overwrite any name in your module.
For most real tasks you do not need exec at all.
runpy.run_path() from the standard library runs a script the same way python script.py does and returns its global variables as a dictionary.
It sets __file__ and __name__ for you (the default run_name is "<run_path>"; pass run_name="__main__" to trigger if __name__ == "__main__": blocks). You can seed variables with init_globals:
When the file is a module with functions and classes you want to reuse, load it with importlib so it is cached in sys.modules and behaves like a normal module:
If the file sits on sys.path, a plain import script is simpler still.
| Goal | Use |
|---|---|
| Run a script and read its variables | runpy.run_path() |
| Reuse functions and classes from a file | importlib or plain import |
| Run code from a string or code object | exec() with an explicit dictionary |
| Load configuration | A data format such as JSON, TOML (tomllib) or YAML instead of Python code |
Executing a file runs whatever it contains with your full privileges. Never run files or strings from users or from the network. exec with restricted __builtins__ is not a sandbox. For configuration, prefer data formats, and for untrusted code use a separate process with operating system level isolation.
No. It was removed in Python 3.0, and calling it raises a NameError. Use runpy.run_path() or exec(compile(...)).
The closest equivalent is exec(compile(source, filename, "exec"), namespace). For running whole scripts, runpy.run_path() is the cleaner standard-library choice.
Pass a fresh dictionary as the globals argument, or use the dictionary returned by runpy.run_path(). Read results from that dictionary.
You passed the source text without a filename. Call compile(source, filename, "exec") first so tracebacks reference the real file.
execfile ran a Python file in a namespace but exists only in Python 2. In Python 3, use runpy.run_path() to run scripts, importlib or import to load modules, and exec(compile(...), namespace) when you need full control. Always use an explicit namespace and never execute untrusted code.