run program in Python shell


Question

I have a demo file: test.py. In the Windows Console I can run the file with: C:\>test.py

How can I execute the file in the Python Shell instead?

1
43
5/27/2018 5:09:45 PM

Accepted Answer

Use execfile for Python 2:

>>> execfile('C:\\test.py')

Use exec for Python 3

>>> exec(open("C:\\test.py").read())
89
9/28/2017 8:15:23 PM

If you're wanting to run the script and end at a prompt (so you can inspect variables, etc), then use:

python -i test.py

That will run the script and then drop you into a Python interpreter.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon