7

Is it possible (not necessarly using python introspection) to print the source code of a script?

I want to execute a short python script that also print its source (so I can see which commands are executed).

The script is something like this:

command1()
#command2()
command3()

print some_variable_that_contain_src

The real application is that I want to run a script from IPython with the run -i magic and have as output the source (i.e. the commands executed). In this way I can check which commands are commented at every execution. Moreover, if executed in a Notebook I leave a trace of which commands have been used.

Solution

Using korylprince solution I end up with this one-liner to be put at the beginning of the script:

with open(__file__) as f: print '\n'.join(f.read().split('\n')[1:])

This will print the script source except the first line (that would be only noise). It's also easy to modify the slicing in order to print a different "slice" of the script.

If you want to print the whole file instead, the one-liner simplifies to:

with open(__file__) as f: print f.read()
Community
  • 1
  • 1
user2304916
  • 7,882
  • 5
  • 39
  • 53
  • maybe look into the inspect package in python: http://www.python.org/doc//current/library/inspect.html. Specifically the `getsource` method – Greg Aug 20 '13 at 02:22
  • This type of programs are called Quine programs. http://en.wikipedia.org/wiki/Quine_(computing) I am trying to write one myself in python, its too damn difficult. :( – thefourtheye Aug 20 '13 at 03:06
  • 1
    Why remove the first line? You get a script that, when run, prints out source code *that doesn't contain anything about printing its source code*. It's confusing. The first line isn't noise any more than the comments or asserts are noise. – user2357112 Aug 20 '13 at 03:13
  • @user2357112: it's a matter of abstract orthogonality vs practical convenience. I just want to have visual feedback of the commands used in some data analisys. I discards all the non-interesting part to have a cleaner output. – user2304916 Aug 20 '13 at 19:10

2 Answers2

14

As long as you're not doing anything crazy with packages, put this at the top of your script

with open(__file__) as f:
    print f.read()

Which will read in the current file and print it out.

For python 3 make sure to use instead print(f.read())

Mark
  • 1,337
  • 23
  • 34
korylprince
  • 2,969
  • 1
  • 18
  • 27
  • Yes, this works only if put at the beginning of the script. If put at the end and I execute other scripts the last executed file will be printed. – user2304916 Aug 20 '13 at 02:41
5

For the most simple answer:

import my_module

print open(my_module.__file__).read()

I also tried using the inspect package.

import inspect

import my_module

source_list = inspect.getsourcelines(my_module)

Will give you a list of strings with the source code defined in it

for line in source_list[0]:
    print line

Will print out the entire source code in a readable manner

Greg
  • 5,422
  • 1
  • 27
  • 32