Given a file path, determine the full module path.

    e.g. '/usr/lib/python2.7/dist-packages/numpy/core/__init__.pyc' yields
    'numpy.core'
19360
def get_parent_until(path):
    
    dirname = osp.dirname(path)
    try:
        mod = osp.basename(path)
        mod = osp.splitext(mod)[0]
        imp.find_module(mod, [dirname])
    except ImportError:
        return
    items = [mod]
    while 1:
        items.append(osp.basename(dirname))
        try:
            dirname = osp.dirname(dirname)
            imp.find_module('__init__', [dirname + os.sep])
        except ImportError:
            break
    return '.'.join(reversed(items))
861792
def _get_relative_pythonpath(self):
        """"""
        # Workaround to replace os.path.relpath (new in Python v2.6):
        offset = len(self.root_path)+len(os.pathsep)
        return [path[offset:] for path in self.pythonpath]
687590
def get_module_path(modname):
    """"""
    return osp.abspath(osp.dirname(sys.modules[modname].__file__))
256334
def find_path_package_name(thepath):
    
    module_found = False
    last_module_found = None
    continue_ = True
    while continue_:
        module_found = is_path_python_module(thepath)
        next_path = path.dirname(thepath)
        if next_path == thepath:
            continue_ = False
        if module_found:
            init_names = ['__init__%s' % suffix.lower() for suffix in _py_suffixes]
            if path.basename(thepath).lower() in init_names:
                last_module_found = path.basename(path.dirname(thepath))
            else:
                last_module_found = path.basename(thepath)
        if last_module_found and not module_found:
            continue_ = False
        thepath = next_path
    return last_module_found
823264
def find_spec(self, fullname, path, target=None):
        
        if fullname.startswith(self.package_prefix):
            for path in self._get_paths(fullname):
                if os.path.exists(path):
                    return ModuleSpec(
                        name=fullname,
                        loader=self.loader_class(fullname, path),
                        origin=path,
                        is_package=(path.endswith('__init__.ipynb') or path.endswith('__init__.py')),
                    )
200
def find_module(modpath):
    """"""
    module_path = modpath.replace('.', '/') + '.py'
    init_path = modpath.replace('.', '/') + '/__init__.py'
    for root_path in sys.path:
        path = os.path.join(root_path, module_path)
        if os.path.isfile(path):
            return path
        path = os.path.join(root_path, init_path)
        if os.path.isfile(path):
            return path
118294
def get_modules(paths, toplevel=True):
    """"""
    modules = []
    for path in paths:
        path = os.path.abspath(path)
        if toplevel and path.endswith('.pyc'):
            sys.exit('.pyc files are not supported: {0}'.format(path))
        if os.path.isfile(path) and (path.endswith('.py') or toplevel):
            modules.append(path)
        elif os.path.isdir(path):
            subpaths = [
                os.path.join(path, filename)
                for filename in sorted(os.listdir(path))]
            modules.extend(get_modules(subpaths, toplevel=False))
        elif toplevel:
            sys.exit('Error: {0} could not be found.'.format(path))
    return modules
465799
def _find_module(name, path=None):
    

    parts = name.split('.')

    for part in parts:
        if path is not None:
            path = [path]

        fh, path, descr = imp.find_module(part, path)
        if fh is not None and part != parts[-1]:
            fh.close()

    return fh, path, descr
673036
def isPackage(file_path):
    
    return (os.path.isdir(file_path) and
            os.path.isfile(os.path.join(file_path, '__init__.py')))
830770
def get_module_path(module_name):
    
    path = sys.path
    for name in module_name.split('.'):
        file_pointer, path, desc = imp.find_module(name, path)
        path = [path, ]
        if file_pointer is not None:
            file_pointer.close()

    return path[0]
1160693
def GetPythonLibraryDirectoryPath():
  """"""
  path = sysconfig.get_python_lib(True)
  _, _, path = path.rpartition(sysconfig.PREFIX)

  if path.startswith(os.sep):
    path = path[1:]

  return path
157945
def find_module_path():
    """"""
    master_modname = __name__.split(".", 1)[0]
    master_module = sys.modules[master_modname]
    #return os.path.dirname(os.path.realpath(os.path.join(inspect.getsourcefile(endpoints), "..")))
    path = os.path.dirname(inspect.getsourcefile(master_module))
    return path
1367479
def get_base_path(path):
        
        if "%" not in path:
            return path

        path = os.path.split(path)[0]

        while "%" in path:
            path = os.path.split(path)[0]

        return path
3075
def fullpath(relpath):
    ''''''
    if (type(relpath) is object or type(relpath) is file):
        relpath = relpath.name
    return os.path.abspath(os.path.expanduser(relpath))
570733
def path_to_module(path, base=nil)
      if base
        path = ::File.expand_path(path, base)
        raise PoisePython::Error.new("Path #{path} is not inside base path #{base}") unless path.start_with?(base)
        path = path[base.length+1..-1]
      end
      path = path[0..-4] if path.end_with?('.py')
      path.gsub(/#{::File::SEPARATOR}/, '.')
    end
660224
def find_spec(self, fullname, path, target=None):
        ''''''
        if not path:
            path = [os.getcwd()]
        if "." in fullname:
            name = fullname.split(".")[-1]
        else:
            name = fullname
        for entry in path:
            if os.path.isdir(os.path.join(entry, name)):
                # this module has child modules
                filename = os.path.join(entry, name, "__init__.py")
                submodule_locations = [os.path.join(entry, name)]
            else:
                filename = os.path.join(entry, name + ".py")
                submodule_locations = None
            if not os.path.exists(filename):
                continue

            return spec_from_file_location(fullname, filename,
                                           loader=MyLoader(filename),
                                           submodule_search_locations=submodule_locations)
        return None
392102
def packageRootPath(path):
    
    path = nstr(path)
    if os.path.isfile(path):
        path = os.path.dirname(path)

    parts = os.path.normpath(path).split(os.path.sep)
    package_parts = []

    for i in range(len(parts), 0, -1):
        filename = os.path.sep.join(parts[:i] + ['__init__.py'])

        if not os.path.isfile(filename):
            break

        package_parts.insert(0, parts[i - 1])

    if not package_parts:
        return path
    return os.path.abspath(os.path.sep.join(parts[:-len(package_parts)]))