Making Python run a few lines before my script
Question: I need to run a script `foo.py`, but I need to also insert some debugging
lines to run before the code in `foo.py`. Currently I just put those lines in
`foo.py` and I'm careful not to commit that to Git, but I don't like this
solution.

What I want is a separate file `bar.py` that I don't commit to Git. Then I
want to run:

    
    
    python /somewhere/bar.py /somewhere_else/foo.py
    

What I want this to do is first run some lines of code in `bar.py`, and then
run `foo.py` as `__main__`. It should be in the same process that the `bar.py`
lines ran in, otherwise the debugging lines won't help.

Is there a way to make `bar.py` do this?

Someone suggested this:

    
    
    import imp
    import sys
    
    # Debugging code here
    
    fp, pathname, description = imp.find_module(sys.argv[1])
    imp.load_module('__main__', fp, pathname, description)
    

The problem with that is that because it uses import machinery, I need to be
on the same folder as `foo.py` to run that. I don't want that. I want to
simply put in the full path to `foo.py`.

Also: The solution needs to work with `.pyc` files as well.


Answer: You can use
[`execfile()`](https://docs.python.org/2/library/functions.html#execfile) if
the file is `.py` and
[uncompyle2](https://github.com/wibiti/uncompyle2/blob/master/scripts/uncompyle2)
if the file is `.pyc`.

Let's say you have your file structure like:

    
    
    test|-- foo.py
        |-- bar
             |--bar.py   
    

**foo.py**

    
    
    import sys
    
    a = 1
    print ('debugging...')
    
    # run the other file
    if sys.argv[1].endswith('.py'): # if .py run right away
        execfile(sys.argv[1], globals(), locals())
    elif sys.argv[1].endswith('.pyc'): # if .pyc, first uncompyle, then run
        import uncompyle2
        from StringIO import StringIO
        f = StringIO()
        uncompyle2.uncompyle_file(sys.argv[1], f)
        f.seek(0)
        exec(f.read(), globals(), locals())
    

**bar.py**

    
    
    print a
    print 'real job'
    

And in `test/`, if you do:

    
    
    $ python foo.py bar/bar.py
    $ python foo.py bar/bar.pyc
    

Both, outputs the same:

    
    
    debugging...
    1
    real job
    

Please also see this [answer](http://stackoverflow.com/a/32261548/547820).

