#!/usr/bin/env python # used in a script with shebang in 1 of 2 ways # #!/path/to/rel relative_interpreter # #!/usr/bin/env rel relative_interpreter # the latter requires support for more than 1 parameter to the intpreter (not on linux) # # will set SCRIPT_FILE and SCRIPT_PATH # example: # #!/usr/local/bin/rel python # or, going with the form that just requires rel be in the path # #!/usr/bin/env rel python # # these assume the script is in the same folder as the python interpreter. other deeper locations are also valid, like: # #!/usr/bin/env rel ../Resources/Python.app/Contents/MacOS/Python # # Many systems only allow 1 parameter with Shebang. On these systems, rel # must be installed in a standard location, then the following syntax can be used # and it will use the interpreter relative to the script's path # #!/usr/bin/rel python # import os,sys import logging LOG = logging.debug def main(): interpreter=sys.argv[1] scriptfile=sys.argv[2] args=[None]+sys.argv[2:] LOG('%s %s' % (interpreter,scriptfile)) scriptpath=os.path.dirname(os.path.realpath(scriptfile)) interpreter=os.path.normpath(os.path.join(scriptpath,interpreter)) LOG('%s %s' % (scriptpath,interpreter)) args[0]=interpreter #print replacements LOG('running interpreter '+interpreter+' with parameters '+repr(args)) LOG('script_path=%s' % (scriptpath)) assert(os.path.exists(interpreter) and not os.path.isdir(interpreter)) assert(os.path.exists(scriptfile) and not os.path.isdir(scriptfile)) assert(os.path.exists(scriptpath) and os.path.isdir(scriptpath)) env=os.environ.copy() env['SCRIPT_FILE']=scriptfile env['SCRIPT_PATH']=scriptpath os.execve(interpreter,args,env) raise RuntimeError('Exec Failed') if __name__ == '__main__': main()