Python Forum

Full Version: executing a file saved in memory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i would like to make a script that reads in a python source file, then later (the file is no longer on this host's filesystem(s)) run that file.

it would be a big plus if i can capture the global and local namespaces that running it produces.

i have already done the above with the exec() function.  i need to further test it to see what scope of sources it can run.

it would be another plus if can, or if there is some way to, capture the output of running this file.  i'm thinking that pipes taking on stdout and stderr output could be a way to do this.

it would be another big plus if it can, or if there is some way to, do all this for .pyc and .pyo files that were read in without writing to a file (on an embedded device, for example, that only has read/only file mounts).  i have no idea if exec() knows how to do this but it would be great if it can.

the form that the file needs to be stored in to be run for all this only needs to be documented.  if it can only be done in python 3.5 and later, that is ok.