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.
🌐
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...
🌐
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()).
🌐
Programiz
programiz.com › python-programming › methods › built-in › exec
Python exec() (With Examples)
The exec() method executes the dynamically created program, which is either a string or a code object. In this tutorial, you will learn about the exec() method with the help of examples.
🌐
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
🌐
pkimber.net
pkimber.net › howto › python › execfile.html
execfile — pkimber.net
execfile('/script/testFunctions.py') print printName('Cathy', 'Smith')
🌐
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 ...
Find elsewhere
🌐
GitHub
github.com › projg2 › python-exec
GitHub - projg2/python-exec: Wrapper for multi-implementation install of Python scripts and executables
In other words, to wrap /usr/bin/foo, you would: 1. install the real script into /usr/lib/python-exec/pythonX.Y/foo, 2. symlink /usr/bin/foo -> /usr/lib/python-exec/python-exec2. Afterwards, whenever the wrapper script is run python-exec will try to determine the most preferred Python implementation supported by the script and run the appropriate variant.
Starred by 14 users
Forked by 6 users
Languages   C 50.3% | Python 38.1% | M4 7.1% | Makefile 4.5% | C 50.3% | Python 38.1% | M4 7.1% | Makefile 4.5%
🌐
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.
🌐
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
🌐
Armin Ronacher
lucumr.pocoo.org › 2011 › 2 › 1 › exec-in-python
Be careful with exec and eval in Python | Armin Ronacher's Thoughts and Writings
February 1, 2011 - So this post aims to clean up some of the misconceptions about the exec keyword (or builtin function in Python 3) and why you have to be careful with using it. This post was inspired by a discussion on reddit about the use of the execfile function in the web2py web framework but also applies ...
🌐
DataFlair
data-flair.training › blogs › python-exec-function
Python exec Function - Example and Risk - DataFlair
November 24, 2018 - Learn what the exec function is in Python with syntax and example, Exec vs eval, Risks with Python Exec - problem & Solution
🌐
Google Groups
groups.google.com › g › comp.lang.python › c › 9UXrw2SZNUU
execfile and argv
Reference manual says: "execfile (file[, globals[, locals]]) This function is similar to the exec statement, but parses a file instead of a string. It is different from the import statement in that it does not use the module administration --- it reads the file unconditionally and does not ...
🌐
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.
🌐
W3Schools
w3schools.com › PYTHON › ref_func_exec.asp
Python exec() Function
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST
🌐
GeeksforGeeks
geeksforgeeks.org › python › exec-in-python
exec() in Python - GeeksforGeeks
November 29, 2023 - Dynamic execution in Python allows the execution of specific methods such as sum() and iter() along with built-in methods inside the exec() function, demonstrating the flexibility of dynamic execution in Python. Through this, only the sum, and iter methods along with all the built-in methods can be executed inside exec() function.
🌐
Gentoo Wiki
wiki.gentoo.org › wiki › Project:Python › python-exec
Project:Python/python-exec - Gentoo wiki
October 20, 2025 - python-exec is the tool used to wrap Python scripts for multiple implementations. In the process of wrapping, the original Python script is replaced by a special wrapper executable that invokes a proper version of the original script depending on which Python interpreter is used/requested · ...