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
execfileexec
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.
Top answer 1 of 13
507
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
execfileexec
2 of 13
247
You are just supposed to read the file and exec the code yourself. 2to3 currently replaces this:
execfile("somefile.py", global_vars, local_vars)
With this:
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 Python's docs for:
execfilecompileexec
Videos
01:16
PYTHON : Alternative to execfile in Python 3? - YouTube
04:09
What is an alternative to execfile in Python 3? - YouTube
01:31
Exploring Alternatives to `execfile` in Python 3 - YouTube
09:17
Be Careful When Using exec() or eval() in Python - YouTube
04:12
Python built-in function: exec() - YouTube
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 ...
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%
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 ...
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 ...
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 · ...
Top answer 1 of 13
60
Actually, wouldn't we want to do this?
import sys
sys.argv = ['abc.py','arg1', 'arg2']
execfile('abc.py')
2 of 13
59
try this:
import sys
sys.argv = ['arg1', 'arg2']
execfile('abc.py')
Note that when abc.py finishes, control will be returned to the calling program. Note too that abc.py can call quit() if indeed finished.