Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- python /somewhere/bar.py /somewhere_else/foo.py
- import imp
- import sys
- # Debugging code here
- fp, pathname, description = imp.find_module(sys.argv[1])
- imp.load_module('__main__', fp, pathname, description)
- "This module is automatically imported during initialization."
- import imp
- import os
- if 'MY_STARTUP_FILE' in os.environ:
- try:
- file_path = os.environ['MY_STARTUP_FILE']
- folder, file_name = os.path.split(file_path)
- module_name, _ = os.path.splitext(file_name)
- fp, pathname, description = imp.find_module(module_name, [folder])
- except Exception as e:
- # Broad exception handling since sitecustomize exceptions are ignored
- print "There was a problem finding startup file", file_path
- print repr(e)
- exit()
- try:
- imp.load_module(module_name, fp, pathname, description)
- except Exception as e:
- print "There was a problem loading startup file: ", file_path
- print repr(e)
- exit()
- finally:
- # "the caller is responsible for closing the file argument" from imp docs
- if fp:
- fp.close()
- MY_STARTUP_FILE=/somewhere/bar.py python /somewhere_else/foo.py
- test|-- foo.py
- |-- bar
- |--bar.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())
- print a
- print 'real job'
- $ python foo.py bar/bar.py
- $ python foo.py bar/bar.pyc
- debugging...
- 1
- real job
- if __name__ == '__main__':
- # some code
- if __name__ == '__main__':
- main()
- if __name__ == "__main__":
- # You debugging setup goes here
- ...
- # Run the Python program given as argv[1]
- run_script_as_main(sys.argv[1:])
- $ find so-foo-bar
- so-foo-bar
- so-foo-bar/debugaid
- so-foo-bar/debugaid/bar.py
- so-foo-bar/foo.py
- import sys
- if __name__ == "__main__":
- print "My name is", __name__
- print "My file is", __file__
- print "My command line is", sys.argv
- print "First 2 path items", sys.path[:2]
- print "Globals", globals().keys()
- raise Exception("foo")
- run_script_as_main
- export PYTHONPATH=$HOME/dirWithFoo/:$PYTHONPATH
- from foo import myfunc
- myfunc()
- imp.load_source('__main__', '../../foo.py')
- #!/usr/bin/env python
- import imp
- import sys
- import os.path
- file_path = sys.argv.pop(1)
- assert os.path.exists(file_path)
- folder, file_name = os.path.split(file_path)
- ###############################################################################
- # #
- # Debugging cruft here
- # #
- ###############################################################################
- module_name, _ = os.path.splitext(file_name)
- sys.path.append(folder)
- imp.load_module('__main__', *imp.find_module(module_name))
- python <(cat /somewhere/bar.py /somewhere_else/foo.py)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement