include a file in a python program

Terry Reedy tjreedy at
Mon Sep 6 01:41:39 CEST 2010

On 9/5/2010 7:12 PM, Steven D'Aprano wrote:

> fp = open("python.txt")
> text =
> fp.close()
> exec(text)
> But keep in mind that the contents of python.txt will be executed as if
> you had typed it yourself. If you don't trust the source with your life
> (or at least with the contents of your computer), don't execute it.

Also, this executes the code within the namespace of the calling code 
rather than within a fresh module namespace. That could be either better 
or worse.

Terry Jan Reedy

More information about the Python-list mailing list