Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › execfile.html
execfile — Python Reference (The Right Way) 0.1 documentation
>>> execfile(r'C:\test.py') #test.py contains the following line: 'print "foo"' foo
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.
Videos
W3Schools
w3schools.com › python › ref_func_exec.asp
Python exec() Function
The exec() function executes the specified Python code. The exec() function accepts large blocks of code, unlike the eval() function which only accepts a single expression ... If you want to use W3Schools services as an educational institution, ...
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
Python Programming
pythonprogramming.net › python-exec-tutorial
Exec with Python Tutorial
This is another example of something people might get the wild idea of using unprotected with some sort online form so they can create their own embedded Python IDE in a browser. Let's see some examples of exec. If you recall from the previous tutorial, eval wouldn't compile any code.
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
YouTube
youtube.com › watch
Exec - Python programming tutorial - YouTube
This function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite ...
Published May 24, 2015
CodeRivers
coderivers.org › blog › python-execfile
Python `execfile` - A Comprehensive Guide - CodeRivers
February 22, 2026 - In Python, the `execfile` function has been a part of the language's toolset for a long time. It allows you to execute Python code from an external file within your Python script. This can be extremely useful in various scenarios, such as modularizing code, reusing common code snippets across ...
TutorialsPoint
tutorialspoint.com › exec-in-python
exec() in Python
Exec function can dynamically execute code of python programs. The code can be passed in as string or object code to this function. The object code is executed as is while the string is first parsed and checked for any syntax error. If no syntax e
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.
Sololearn
sololearn.com › en › Discuss › 553584 › how-do-i-make-a-python-file-executable-from-the-console
How do i make a python file executable from the console? | Sololearn: Learn to code for FREE!
If you save your python file (e.g. "your_file.py") in the same directory as python.exe, you can run it from the console with the command: >>>exec(open("your_file.py").read()) You can also write your file there with (e.g.) >>>newfile = open("your_file.py", "w") >>>newfile.write("print('hello world!')") >>>newfile.close() You can check to make sure it is there with >>>import os >>>os.listdir() You should get something like this: ['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python36.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll', 'your_file.py'] This is what you get when you run it: >>>exec(open("your_file.py").read()) hello world! ... The function execfile() doesnt exist in Python 3.
pkimber.net
pkimber.net › howto › python › execfile.html
execfile — pkimber.net
Python · execfile · View page source · Calling scripts using another script · Use the execfile command to call a script from another script. Create a script called testFunctions.py that contains the following: def printName(first, last): name = first + ' ' + last return name ·
Nmt
cs.nmt.edu › ~jeffery › Shipman › www › docs › tcc › help › pubs › python27 › web › execfile-function.html
21.7. execfile(): Execute a Python source file
To execute a sequence of Python statements in some file F, use this function: execfile(F) The function returns None. For additional features that allow you to control the environment of the executed statements, see the official documentation. Next: 21.8. getattr(): Retrieve an attribute of ...
Readthedocs
casadocs.readthedocs.io › en › stable › api › casashell › execfile.html
execfile — CASAdocs documentation
When execfile is used within the filename being evaluated, it is necessary to add globals( ) as the second argument to those execfile calls in order for the secondary script to know about the global variables of the calling script. For example, within a script ‘mainscript.py’, calls to another script ‘myscript.py’ should be written as execfile(‘myscript.py’, globals()).