How to execute a file within the Python interpreter?

Several ways.

  • From the shell

    python someFile.py
    
  • From 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
    

Leave a Comment