Several ways.
From the shell
python someFile.pyFrom 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
Several ways.
From the shell
python someFile.pyFrom 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
For Python 2:
>>> execfile('filename.py')
For Python 3:
>>> exec(open("filename.py").read())
# or
>>> from pathlib import Path
>>> exec(Path("filename.py").read_text())
See the documentation. If you are using Python 3.0, see this question.
See answer by @S.Lott for an example of how you access globals from filename.py after executing it.
How to I execute a .py file from the interpreter?
What alternative is there to execfile in Python 3? / How to include a Python file? - Stack Overflow
java - Adding arguments in Jython PythonInterpreter to the "execfile" function - Stack Overflow
Execute a file with arguments in Python shell - Stack Overflow
Videos
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.
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
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
Actually, wouldn't we want to do this?
import sys
sys.argv = ['abc.py','arg1', 'arg2']
execfile('abc.py')
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.