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
🌐
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.

People also ask

Can I execute a Python file in the interactive interpreter without saving it?
A: Yes, you can use the exec function to execute code directly from a string or from a read file object, as shown in several methods mentioned above.
🌐
sqlpey.com
sqlpey.com › python › top-4-ways-to-execute-file-in-python-interpreter
Top 4 Ways to Execute a File Within the Python Interpreter - …
What is the difference between `import` and using `exec`?
A: import allows you to bring in a module and access its contents through the module’s namespace, while exec runs code dynamically, allowing access to globals directly in the interpreter’s scope.
🌐
sqlpey.com
sqlpey.com › python › top-4-ways-to-execute-file-in-python-interpreter
Top 4 Ways to Execute a File Within the Python Interpreter - …
What happens if my script has side effects?
A: If your script has actions that occur on import, those side effects will execute immediately upon importing the file. To avoid this, it is recommended to wrap such code in functions.
🌐
sqlpey.com
sqlpey.com › python › top-4-ways-to-execute-file-in-python-interpreter
Top 4 Ways to Execute a File Within the Python Interpreter - …
🌐
Tabnine
tabnine.com › home › code library
execfile
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
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.
🌐
docs.python.org
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.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...
Find elsewhere
🌐
Gentoo Wiki
wiki.gentoo.org › wiki › Project:Python › python-exec
Project:Python/python-exec - Gentoo wiki
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.
🌐
sqlpey
sqlpey.com › python › top-4-ways-to-execute-file-in-python-interpreter
Top 4 Ways to Execute a File Within the Python Interpreter - …
December 5, 2024 - Executing a Python file within the interpreter can be a thoughtful solution for various development scenarios. Whether you’re looking to utilize predefined variables or make use of functions from an external script, here’s a deep dive into effective methods for executing a file in Python. If you’re working with Python 2, the straightforward way to execute a file is through the execfile function.
🌐
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 · GitHub
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 15 users
Forked by 6 users
Languages   C 50.3% | Python 38.1% | M4 7.1% | Makefile 4.5%
🌐
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.
🌐
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.
🌐
DataFlair
data-flair.training › blogs › python-exec-function
Python exec Function - Example and Risk - DataFlair
April 14, 2026 - Learn what the exec function is in Python with syntax and example, Exec vs eval, Risks with Python Exec - problem & Solution
🌐
Geek Python
geekpython.in › exec-function-in-python
exec() Function In Python - Detailed Guide With Code
August 15, 2023 - It works exactly like a Python interpreter, taking our code, processing it internally, executing it, and returning the correct results.
🌐
Javadoc.io
javadoc.io › static › org.python › jython-standalone › 2.7.1 › org › python › util › PythonInterpreter.html
PythonInterpreter (Jython API documentation)
Sets a Python object to use for the standard output stream, sys.stdout. This stream is used in a byte-oriented way (mostly) that depends on the type of file-like object.
🌐
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
🌐
Vultr Docs
docs.vultr.com › python › built-in › exec
Python exec() - Execute Dynamically | Vultr Docs
September 27, 2024 - The exec() function in Python is a dynamic and flexible tool for executing Python code programmatically. It allows for execution of complex Python statements, management of execution contexts, and isolation of variable scopes.