Several ways.

  • From the shell

    python someFile.py
    
  • From inside IDLE, hit F5.

  • If you're typing interactively, try this (Python3):

    >>> exec(open("filename.py").read())
    
  • For Python 2:

    >>> variables= {}
    >>> execfile( "someFile.py", variables )
    >>> print variables # globals from the someFile module
    
Answer from S.Lott on Stack Overflow
Discussions

How to I execute a .py file from the interpreter?
I'm assuming that the course is talking about running python3 -i report.py, which will run report.py normally and then drop you into a python interpreter after the program is done running. This python interpreter will have all of your python file's functions, variables, etc so that you can check the value of things. It's an interesting feature for debugging, although I don't use it personally More on reddit.com
🌐 r/learnpython
9
2
June 6, 2021
What alternative is there to execfile in Python 3? / How to include a Python file? - Stack Overflow
It seems like in Python 3 they've removed all of the easy ways to quickly load a script, by removing execfile(). What alternative is there to include a Python file in another one, and execute it? More on stackoverflow.com
🌐 stackoverflow.com
java - Adding arguments in Jython PythonInterpreter to the "execfile" function - Stack Overflow
I have a python script that I would like to execute in Java with Jython. The Python script accepts 2 arguments. How can I add arguments to the script? PythonInterpreter interpreter = new More on stackoverflow.com
🌐 stackoverflow.com
March 3, 2017
Execute a file with arguments in Python shell - Stack Overflow
@marco: The idea is that a “script” ... line. execfile runs in the current process, so (if it’s relevant) uses the current command line. In that sense, you can’t use it without “passing” arguments! 2019-04-03T03:17:11.763Z+00:00 ... You're confusing loading a module into the current interpreter process and calling a Python script ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Tabnine
tabnine.com › home page › code › java › org.python.util.pythoninterpreter
Java Examples & Tutorials of PythonInterpreter.execfile (org.python.util) | Tabnine
import org.python.util.PythonInterpreter; public class JythonTest { public static void main(String[] args) { PythonInterpreter interp = new PythonInterpreter(); interp.execfile("src/script.py"); } } ... import org.python.core.PyObject; import org.python.core.PyString; import org.python.util.PythonInterpreter; public class method { public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("/pathtoyourmodule/somme_x_y.py"); PyObject str = interpreter.eval("repr(somme(4,5))"); System.out.println(str.toString()); }
🌐
Reddit
reddit.com › r/learnpython › how to i execute a .py file from the interpreter?
r/learnpython on Reddit: How to I execute a .py file from the interpreter?
June 6, 2021 -

I have been at this for an hour. I wrote the script, went line by line in interactive mode (without opening the file) and it worked perfectly. Now I'm trying to actually run the file from the python (>>>) interpreter so that I can interact with it directly. I'm in the right directory. I'm using both Powershell and Kali Linux. I was trying first with powershell, but since the course is Linux based, I switched to my Kali Linux VM. Same.

The course says "Hint: Use -i when executing the file in the terminal"

I can execute it from the terminal, but not interactively in the python interpreter.

Please have mercy on me. I've looked everywhere and exec() doesn't work.

exec(report.py)

NameError : name 'report' is not defined

If it matters, the code defines a function that uses csv to enter data into a list of tuples. The function takes one argument : a csv file. I did all the sololearn courses in python and am now working through https://github.com/dabeaz-course/practical-python .

I also took python for biologists, which was too easy. But it taught me to be proficient in using vim, and that's what I used to create the file.

🌐
Program Creek
programcreek.com › java-api-examples
org.python.util.PythonInterpreter#execfile
The following examples show how to use org.python.util.PythonInterpreter#execfile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar. ... @Override public void process() { try { PythonInterpreter interpreter = python.interpreter(); interpreter.execfile(filename); PyFunction check = interpreter.get("check", PyFunction.class); PyObject check_call = check.__call__(new PyString(ip), new PyInteger(port), new PyInteger(timeout)); Str
🌐
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.
Find elsewhere
🌐
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.
🌐
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 - A python developer depends on imports doing what they are documented to do and that the namespace has a specific initial value (namely that it’s empty with the exception of a few internal variables such as __name__, __loader__ etc.). A Python developer depends on being able to import that module by a dotted name, on the fact that modules shut down in a specific way, that they are cached in sys.modules etc. Jacob Kaplan-Moss wrote a comment on Reddit about the use of exec in web2py a while ago which I would recommend reading. He brings up some very good points why changing the semantics of a language is a bad idea. However web2py and it’s use of execfile are not the only offenders in the Python web community.
🌐
Google Groups
groups.google.com › g › comp.lang.python › c › VXZYq1DAVqY › m › -UQBMJhVG_wJ
How to run script from interpreter?
On Wed, 28 May 2014 11:39:23 -0500, Mark H Harris wrote: > On 5/28/14 2:44 AM, onlyv...@gmail.com wrote: >> On Friday, January 19, 2001 1:22:23 AM UTC+5:30, Rolander, Dan wrote: >>> What is the best way to run a python script from within the .................................^^^^^^^^ >>> interpreter? What command should I use? >>> >>> >> try using execfile(filename) >> >> > What type of script?
🌐
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.
🌐
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...
🌐
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
🌐
DEV Community
dev.to › sachingeek › how-to-use-exec-in-python-everything-you-need-to-know-380f
How to Use exec() in Python - Everything You Need to Know - DEV Community
July 30, 2023 - Think of a Python interpreter that takes the piece of code, processes it internally, and executes it, the exec() function does the same.
🌐
GitHub
github.com › projg2 › python-exec
GitHub - projg2/python-exec: Wrapper for multi-implementation install of Python scripts and executables
If the specified interpreter is unsupported by the script, python-exec will fall back to configuration file and/or default order. The configuration file (example in config/python-exec.conf.example) needs to be installed in ${sysconfdir}/python-exec/python-exec.conf.
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%
🌐
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.