def prepare_exec_for_file(filename):
 module = []
 if (os.path.split(filename)[1] == '__init__.py'):
  filename = os.path.dirname(filename)
 elif filename.endswith('.py'):
  filename = filename[:(-3)]
 else:
  raise NoAppException(('The file provided (%s) does exist but is not a valid Python file.  This means that it cannot be used as application.  Please change the extension to .py' % filename))
 filename = os.path.realpath(filename)
 dirpath = filename
 while 1:
  (dirpath, extra) = os.path.split(dirpath)
  module.append(extra)
  if (not os.path.isfile(os.path.join(dirpath, '__init__.py'))):
   break
 sys.path.insert(0, dirpath)
 return '.'.join(module[::(-1)])

def prepare_exec_for_file ( filename ) : module = [ ] if ( os . path . split ( filename ) [ 1 ] == '__init__.py' ) : filename = os . path . dirname ( filename ) elif filename . endswith ( '.py' ) : filename = filename [ : ( - 3 ) ] else : raise NoAppException ( ( 'The file provided (%s) does exist but is not a valid Python file.  This means that it cannot be used as application.  Please change the extension to .py' % filename ) ) filename = os . path . realpath ( filename ) dirpath = filename while 1 : ( dirpath , extra ) = os . path . split ( dirpath ) module . append ( extra ) if ( not os . path . isfile ( os . path . join ( dirpath , '__init__.py' ) ) ) : break sys . path . insert ( 0 , dirpath ) return '.' . join ( module [ : : ( - 1 ) ] )
given a filename this will try to calculate the python path .