Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. python /somewhere/bar.py /somewhere_else/foo.py
  2.  
  3. import imp
  4. import sys
  5.  
  6. # Debugging code here
  7.  
  8. fp, pathname, description = imp.find_module(sys.argv[1])
  9. imp.load_module('__main__', fp, pathname, description)
  10.  
  11. "This module is automatically imported during initialization."
  12.  
  13. import imp
  14.  
  15. import os
  16.  
  17. if 'MY_STARTUP_FILE' in os.environ:
  18. try:
  19. file_path = os.environ['MY_STARTUP_FILE']
  20. folder, file_name = os.path.split(file_path)
  21. module_name, _ = os.path.splitext(file_name)
  22. fp, pathname, description = imp.find_module(module_name, [folder])
  23. except Exception as e:
  24. # Broad exception handling since sitecustomize exceptions are ignored
  25. print "There was a problem finding startup file", file_path
  26. print repr(e)
  27. exit()
  28.  
  29. try:
  30. imp.load_module(module_name, fp, pathname, description)
  31. except Exception as e:
  32. print "There was a problem loading startup file: ", file_path
  33. print repr(e)
  34. exit()
  35. finally:
  36. # "the caller is responsible for closing the file argument" from imp docs
  37. if fp:
  38. fp.close()
  39.  
  40. MY_STARTUP_FILE=/somewhere/bar.py python /somewhere_else/foo.py
  41.  
  42. test|-- foo.py
  43. |-- bar
  44. |--bar.py
  45.  
  46. import sys
  47.  
  48. a = 1
  49. print ('debugging...')
  50.  
  51. # run the other file
  52. if sys.argv[1].endswith('.py'): # if .py run right away
  53. execfile(sys.argv[1], globals(), locals())
  54. elif sys.argv[1].endswith('.pyc'): # if .pyc, first uncompyle, then run
  55. import uncompyle2
  56. from StringIO import StringIO
  57. f = StringIO()
  58. uncompyle2.uncompyle_file(sys.argv[1], f)
  59. f.seek(0)
  60. exec(f.read(), globals(), locals())
  61.  
  62. print a
  63. print 'real job'
  64.  
  65. $ python foo.py bar/bar.py
  66. $ python foo.py bar/bar.pyc
  67.  
  68. debugging...
  69. 1
  70. real job
  71.  
  72. if __name__ == '__main__':
  73. # some code
  74.  
  75. if __name__ == '__main__':
  76. main()
  77.  
  78. if __name__ == "__main__":
  79. # You debugging setup goes here
  80. ...
  81. # Run the Python program given as argv[1]
  82. run_script_as_main(sys.argv[1:])
  83.  
  84. $ find so-foo-bar
  85. so-foo-bar
  86. so-foo-bar/debugaid
  87. so-foo-bar/debugaid/bar.py
  88. so-foo-bar/foo.py
  89.  
  90. import sys
  91.  
  92. if __name__ == "__main__":
  93. print "My name is", __name__
  94. print "My file is", __file__
  95. print "My command line is", sys.argv
  96. print "First 2 path items", sys.path[:2]
  97. print "Globals", globals().keys()
  98. raise Exception("foo")
  99.  
  100. run_script_as_main
  101.  
  102. export PYTHONPATH=$HOME/dirWithFoo/:$PYTHONPATH
  103.  
  104. from foo import myfunc
  105.  
  106. myfunc()
  107.  
  108. imp.load_source('__main__', '../../foo.py')
  109.  
  110. #!/usr/bin/env python
  111.  
  112. import imp
  113. import sys
  114. import os.path
  115.  
  116. file_path = sys.argv.pop(1)
  117. assert os.path.exists(file_path)
  118.  
  119. folder, file_name = os.path.split(file_path)
  120.  
  121. ###############################################################################
  122. # #
  123.  
  124. # Debugging cruft here
  125.  
  126. # #
  127. ###############################################################################
  128.  
  129.  
  130. module_name, _ = os.path.splitext(file_name)
  131. sys.path.append(folder)
  132. imp.load_module('__main__', *imp.find_module(module_name))
  133.  
  134. python <(cat /somewhere/bar.py /somewhere_else/foo.py)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement