def module_name_for_filename(filename):
 all_segments = filename.split(os.sep)
 path_elements = all_segments[:(-1)]
 module_elements = [all_segments[(-1)].rsplit('.', 1)[0]]
 while True:
  init_path = os.path.join(*(path_elements + ['__init__.py']))
  if (path_elements[0] is ''):
   init_path = ('/' + init_path)
  if os.path.exists(init_path):
   module_elements.insert(0, path_elements.pop())
  else:
   break
 modulename = '.'.join(module_elements)
 basename = '/'.join(path_elements)
 return (basename, modulename)

def module_name_for_filename ( filename ) : all_segments = filename . split ( os . sep ) path_elements = all_segments [ : ( - 1 ) ] module_elements = [ all_segments [ ( - 1 ) ] . rsplit ( '.' , 1 ) [ 0 ] ] while True : init_path = os . path . join ( * ( path_elements + [ '__init__.py' ] ) ) if ( path_elements [ 0 ] is '' ) : init_path = ( '/' + init_path ) if os . path . exists ( init_path ) : module_elements . insert ( 0 , path_elements . pop ( ) ) else : break modulename = '.' . join ( module_elements ) basename = '/' . join ( path_elements ) return ( basename , modulename )
given the name of a python file .