According to the Python documentation, instead of this:

execfile("./filename") 

Use this:

exec(open("./filename").read())

See Python's docs for:

  • What’s New In Python 3.0
  • execfile
  • exec
Answer from user1882644 on Stack Overflow
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › execfile.html
execfile — Python Reference (The Right Way) 0.1 documentation
execfile (filename[, globals[, locals]]) filename · Required. A file to be parsed and evaluated as a sequence of Python statements (similarly to a module). globals · Optional. Any mapping object providing global namespace. locals · Optional. Any mapping object providing local namespace.
🌐
Docker Docs
docs.docker.com › reference › dockerfile reference
Dockerfile reference | Docker Docs
# syntax=docker/dockerfile:1 FROM python:3 RUN pip install awscli RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \ aws s3 cp s3://... ...
🌐
Readthedocs
casadocs.readthedocs.io › en › stable › api › casashell › execfile.html
execfile — CASAdocs documentation
For example, within a script ‘mainscript.py’, calls to another script ‘myscript.py’ should be written as execfile(‘myscript.py’, globals()).
🌐
GitHub
gist.github.com › sunsided › 8278493da89a716835d6a5ab0a51e47e
Fix for execfile missing in Python 3 · GitHub
Fix for execfile missing in Python 3. GitHub Gist: instantly share code, notes, and snippets.
🌐
O'Reilly
oreilly.com › library › view › python-essential-reference › 0672328623 › 0672328623_ch06lev1sec13.html
Python: Essential Reference, Third Edition
February 20, 2006 - The code supplied to exec is executed as if the code actually appeared in place of the exec statement. For example: ... Finally, the execfile(filename [,globals [,locals]]) function executes the contents of a file.
Author   David Beazley
Published   2006
Pages   648
Find elsewhere
🌐
Arch Linux Forums
bbs.archlinux.org › viewtopic.php
python, import and execfile / Programming & Scripting / Arch Linux Forums
October 17, 2005 - That is the code that execfile()'s the file and then inits the class that was inside. It's probably a messy way to do it but Python doens't have variable variables like PHP and it's the best I could come up with on short notice. If I have an import statement inside the file that I pass to ...
🌐
Real Python
realpython.com › ref › stdlib › fileinput
fileinput | Python Standard Library – Real Python
In this tutorial, you'll learn about reading and writing files in Python. You'll cover everything from what a file is made up of to which libraries can help you along that way.
🌐
Python documentation
docs.python.org › 3 › reference › executionmodel.html
4. Execution model — Python 3.14.4 documentation
Structure of a program: A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, ...
🌐
docs.python.org
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.11.5 documentation
The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.,,,, Built-in Functions,,, A, abs(), aiter(), all(), a...
🌐
Real Python
realpython.com › python-exec
Python's exec(): Execute Dynamically Generated Code – Real Python
November 10, 2022 - In this tutorial, you'll learn how to use Python's built-in exec() function to execute code that comes as either a string or a compiled code object.
🌐
Python
bugs.python.org › issue16781
Issue 16781: execfile/exec execution in other than global scope uses locals(), leading to undefined behavior - Python tracker
December 26, 2012 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/60985
🌐
Real Python
realpython.com › ref › builtin-functions › exec
exec() | Python’s Built-in Functions – Real Python
The built-in exec() function allows for the dynamic execution of Python code, which can be provided as either a string or a compiled code object.
🌐
Medium
koshurai.medium.com › exploring-pythons-exec-function-a-simple-guide-c68b421f39d9
Exploring Python’s exec() Function: A Simple Guide | by KoshurAI | Medium
March 1, 2024 - Exploring Python’s exec() Function: A Simple Guide In the world of Python programming, there’s a handy tool called exec(). It's like a magic wand that lets you run Python code on the fly. Let's …
🌐
Google
google.github.io › adk-docs
Agent Development Kit (ADK) - Agent Development Kit (ADK)
1 month ago - New Releases! Explore ADK Python 2.0 Beta with workflows and agent teams, and ADK TypeScript 1.0 is now available
🌐
GeeksforGeeks
geeksforgeeks.org › operating systems › introduction-of-system-call
System Call - GeeksforGeeks
December 15, 2025 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.4 documentation
In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any excep...