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
  • execfile
  • exec
Answer from user1882644 on Stack Overflow
🌐
Real Python
realpython.com › python-exec
Python's exec(): Execute Dynamically Generated Code – Real Python
November 10, 2022 - In all cases, the input string should contain valid Python code. If exec() finds any invalid syntax during the parsing and compilation steps, then the input code won’t run: ... >>> exec("print('Hello, World!)") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 print('Hello, World!)
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › execfile.html
execfile — Python Reference (The Right Way) 0.1 documentation
A file to be parsed and evaluated as a sequence of Python statements (similarly to a module). ... Optional. Any mapping object providing global namespace. ... Optional. Any mapping object providing local namespace. ... This function is similar to the exec statement, but parses a file instead ...
🌐
Programiz
programiz.com › python-programming › methods › built-in › exec
Python exec() (With Examples)
The method executes the python code inside the object method and produces the output Sum = 15. # get an entire program as input program = input('Enter a program:') ... Here, we have used the exec() method to execute the program object which has the user input code. Note: When you use the exec() method with the OS module, you need to be careful. This is because you can accidentally change and delete files ...
🌐
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.
🌐
Codecademy
codecademy.com › docs › python › built-in functions › exec()
Python | Built-in Functions | exec() | Codecademy
August 24, 2023 - The content of the file code.txt is read until the end of the file (EOF) into a string. The content (Python commands) is executed by exec().
🌐
docs.python.org
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.11.5 documentation
The filename argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file ('' is commonly used). The mode argument specifies what kind of code must be compiled; it can be 'exec' if source consists of a sequence of statements, ...
Find elsewhere
🌐
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.

🌐
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 - We can use the exec() function to execute the code from Python(.py) source file by reading the content of the file using the open() function.
🌐
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 - We can use the exec() function to execute the code from Python(.py) source file by reading the content of the file using the open() function.
🌐
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!
🌐
W3Schools
w3schools.com › python › ref_func_exec.asp
Python exec() Function
Python Overview Python Built-in ... Plan Python Interview Q&A Python Bootcamp Python Training ... The exec() function executes the specified Python code....
🌐
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. Because the code variable is actually an object of type code and not a string. The second argument to compile is the filename hint.
🌐
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 - Dynamic Code: If you’re generating code on the fly, exec() lets you run it without having to save it to a file or define it in your script. Script Execution: You can use exec() to run Python scripts stored as strings, which can be useful for ...
🌐
Tutorialspoint
tutorialspoint.com › python › python_exec_function.htm
Python exec() Function
The Python exec() function allows dynamic execution of Python code. It can execute a string containing Python statements or a compiled code object. Generally, this function is used for running code generated by another part of the program.
🌐
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
🌐
DataFlair
data-flair.training › blogs › python-exec-function
Python exec Function - Example and Risk - DataFlair
November 24, 2018 - ... Let’s check the help for this. ... Help on built-in function exec in module builtins: exec(source, globals=None, locals=None, /) Execute the given source in the context of globals and locals.
🌐
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 - The exec() function will run all the contents of the file filename.py on the computer on which it is executed. Think of what you could do with this! Someone could deploy whole projects on another machine if they’ve gained access to the exec() ...