LLI Wiki — My Digital Knowledge Base

Python: run a script file from the interpreter (execfile vs exec)

Replace execfile (Python 2) with exec(open(...).read()) in Python 3.

To run a Python file from the interactive interpreter, execfile only exists in Python 2. In Python 3, use exec with the file content.

# Python 2
execfile("./filename")

# Python 3
exec(open("./filename").read())

The file runs in the current namespace, so its functions and variables remain available in the session.

Mastodon LLIMastodon SocialFosstodon