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

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -