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
🌐
Real Python
realpython.com › python-exec
Python's exec(): Execute Dynamically Generated Code – Real Python
November 10, 2022 - In this example, you use an infinite while loop to mimic the behavior of a Python interpreter or REPL. Inside the loop, you use input() to get the user’s input at the command line.
Discussions

How can I use exec in functions in python3.6 similar to python2.7?
Hello, in python 2.7 the following code works: def f( b ): … exec( ‘a=%s’%b ) … print ‘a=, a … f( 1 ) a= 1 in python3.6 this works in the interpreter: b=1 exec(‘a=%s’%b) a 1 but not in a function: def f( b ): … exec( ‘a=%s’%b ) … print( ‘a=’, a ) … f( 1 ) Traceback ... More on discuss.python.org
🌐 discuss.python.org
0
0
August 17, 2021
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
Why is it advised to not use exec?
If you're thinking about using exec, there are two likely scenarios: 1 - you want to execute some code from a string 2 - you want to let users execute code from an input string In option 1, you are already in control of your code, and there are smarter / saner ways of controlling the code flow / logic than using exec. In option 2, there are security concerns because the user can input bad code and brick their system (or your server, if it's an web app). Consider for example: s = input("write your code:") result = exec(s) print(result) If the user inputs import shutil; shutil.rmtree("/"), say goodbye to your filesystem. More on reddit.com
🌐 r/learnpython
23
23
June 5, 2022
how to run Python 3 script without installing python ??
For Python 3, you want to look into cx_Freeze which can create executables from Python scripts. Depending on your script these may be completely standalone .exe's, or they may be an .exe with some files sitting next to it -- but they can be run without admin permissions (unless the actions you're doing require admin permissions, of course). cx_Freeze works quite nicely; even on Python 2 I prefer it to the other alternatives (py2exe, etc). More on reddit.com
🌐 r/Python
12
1
July 14, 2014
🌐
Gentoo Wiki
wiki.gentoo.org › wiki › Project:Python › python-exec
Project:Python/python-exec - Gentoo wiki
October 20, 2025 - As of 2021, the default python-exec preference follows PYTHON_TARGETS set in profiles. FILE /etc/python-exec/python-exec.confExample system configuration · # prefer python3.14 python3.14 # remaining implementations used in default order #python3.13 #python3.12 #python3.11 # disable pypy3.11 -pypy3.11 · eselect-python provides an easy way to configure preferred/active Python interpreters.
🌐
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 - This function helps us to execute the dynamically generated code. Think of a Python interpreter that takes the piece of code, processes it internally, and executes it, the exec() function does the same.
🌐
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 - But even if the Python interpreter was written in Python it would never pass a string to the exec function. So what would you want to do if you want to get that string into bytecode yourself? You would use the compile builtin: >>> code = compile('a = 1 + 2', '<string>', 'exec') >>> exec code >>> print a 3 · As you can see, exec happily executes bytecode too.
🌐
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. We’re running an infinite loop, and inside it, we’re taking input from the command line and wrapping ...
🌐
Medium
geekpython.medium.com › using-exec-to-execute-dynamically-generated-code-in-python-758ae20b3a7c
Using exec() to execute dynamically generated code in Python | by Sachin Pal | Medium
December 11, 2022 - This function helps us to execute the dynamically generated code. Think of a Python interpreter that takes the piece of code, processes it internally, and executes it, the exec() function does the same.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › exec-in-python
exec() in Python - GeeksforGeeks
November 29, 2023 - If it is a string, the string is parsed as a suite of Python statements which is then executed unless a syntax error occurs and if it is an object code, it is simply executed. We must be careful that the return statements may not be used outside of function definitions not even within the context of code passed to the exec() function. It doesn't return any value, hence returns None. ... In this example, we can see dynamic execution in Python using the exec() function.
🌐
LabEx
labex.io › tutorials › create-code-with-exec-132512
Create Code with Exec | LabEx
This command will start the interactive Python interpreter, where you can directly run Python code. ... Now, we're going to define a piece of Python code as a string and then use the exec() function to execute it.
🌐
Python.org
discuss.python.org › python help
How can I use exec in functions in python3.6 similar to python2.7? - Python Help - Discussions on Python.org
August 17, 2021 - Hello, in python 2.7 the following code works: def f( b ): … exec( ‘a=%s’%b ) … print ‘a=, a … f( 1 ) a= 1 in python3.6 this works in the interpreter: b=1 exec(‘a=%s’%b) a 1 but not in a function: def f( b ): … exec( ‘a=%s’%b ) … print( ‘a=’, a ) … f( 1 ) Traceback ...
🌐
DataFlair
data-flair.training › blogs › python-exec-function
Python exec Function - Example and Risk - DataFlair
November 24, 2018 - While eval returns a value, exec() runs a whole Python statement block and gives back nothing. Feed it a string holding loops, conditionals, or multi-line function definitions, and Python will execute them as if they were part of the file.
🌐
Programiz
programiz.com › python-programming › methods › built-in › exec
Python exec() (With Examples)
# compile the program in execution mode b = compile(program, 'something', 'exec') ... In the above example, we have passed a multi-line program as an argument to the exec() method.
🌐
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.

🌐
Python
docs.python.org › 3.11 › tutorial › interpreter.html
2. Using the Python Interpreter — Python 3.11.15 documentation
These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line. When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This can be done by passing -i before the script. All command line options are described in Command line and environment. When known to the interpreter, the script name and additional arguments thereafter are turned into a list of strings and assigned to the argv variable in the sys module.
🌐
Medium
koshurai.medium.com › exploring-pythons-exec-function-a-simple-guide-c68b421f39d9
Exploring Python’s exec() Function: A Simple Guide | by KoshurAI | Medium
March 1, 2024 - Script Execution: You can use exec() to run Python scripts stored as strings, which can be useful for interpreting scripts within your Python code.
🌐
Vultr Docs
docs.vultr.com › python › built-in › exec
Python exec() - Execute Dynamically | Vultr Docs
September 27, 2024 - Define a string containing a Python expression. Use the exec() function to execute this expression. ... This code snippet executes a string that contains Python code to print "Hello, World!". The output will be the string Hello, World!
🌐
Python documentation
docs.python.org › 3 › tutorial › interpreter.html
2. Using the Python Interpreter — Python 3.14.4 documentation
These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line. When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This can be done by passing -i before the script. All command line options are described in Command line and environment. When known to the interpreter, the script name and additional arguments thereafter are turned into a list of strings and assigned to the argv variable in the sys module.
🌐
Python
docs.python.org › 3.13 › tutorial › interpreter.html
2. Using the Python Interpreter — Python 3.13.13 documentation
These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line. When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This can be done by passing -i before the script. All command line options are described in Command line and environment. When known to the interpreter, the script name and additional arguments thereafter are turned into a list of strings and assigned to the argv variable in the sys module.
🌐
Finxter
blog.finxter.com › home › learn python blog › python exec() — a hacker’s guide to a dangerous function
Python exec() - A Hacker's Guide to A Dangerous Function - Be on the Right Side of Change
June 7, 2022 - program = ''' import random print(random.randint(0,9)) ''' exec(program) # Example Output: 2 · In my execution, the output was 2 but it’ll be different in your case due to the nature of the random function. The point is that you can import all standard Python libraries within an exec() function by using the standard import xxx pattern. This also works if you use the random module in another scope (such as within a function):
🌐
W3Schools
w3schools.com › python › ref_func_exec.asp
Python exec() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... 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, team or enterprise, send us an e-mail: sales@w3schools.com