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()
4
  • maybe look into the inspect package in python: python.org/doc//current/library/inspect.html. Specifically the getsource method Commented Aug 20, 2013 at 2:22
  • This type of programs are called Quine programs. en.wikipedia.org/wiki/Quine_(computing) I am trying to write one myself in python, its too damn difficult. :( Commented Aug 20, 2013 at 3: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. Commented Aug 20, 2013 at 3: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. Commented Aug 20, 2013 at 19:10

2 Answers 2

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())

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

1 Comment

This does not work for a script since I just execute it, not import it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.