run program in Python shell

daniel__ picture daniel__ · Sep 14, 2011 · Viewed 222.1k times · Source

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?

Answer

phihag picture phihag · Sep 14, 2011

Use execfile for Python 2:

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

Use exec for Python 3

>>> exec(open("C:\\test.py").read())