Python script that prints its source -
is possible (not necessarly using python introspection) print source code of script?
i want execute short python script print source (so can see commands executed).
the script this:
command1() #command2() command3() print some_variable_that_contain_src
the real application want run script ipython run -i
magic , have output source (i.e. commands executed). in way can check commands commented @ every execution. moreover, if executed in notebook leave trace of commands have been used.
solution
using korylprince solution end one-liner put @ beginning of script:
with open(__file__) f: print '\n'.join(f.read().split('\n')[1:])
this print script source except first line (that noise). it's easy modify slicing in order print different "slice" of script.
if want print whole file instead, one-liner simplifies to:
with open(__file__) f: print f.read()
as long you're not doing crazy packages, put @ top of script
with open(__file__) f: print f.read()
which read in current file , print out.
Comments
Post a Comment