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 can I use exec in functions in python3.6 similar to python2.7?
How to I execute a .py file from the interpreter?
Why is it advised to not use exec?
how to run Python 3 script without installing python ??
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.