Package logilab :: Package common :: Module modutils
[frames] | no frames]

Module modutils

source code

Python modules manipulation utility functions.
Classes
  NoSourceFile
exception raised when we are not able to get a python source file for a precompiled file
  LazyObject
Functions
module
load_module_from_name(dotted_name, path=None, use_sys=True)
Load a Python module from its name.
source code
module
load_module_from_modpath(parts, path=None, use_sys=True)
Load a python module from its splitted name.
source code
module
load_module_from_file(filepath, path=None, use_sys=True, extrapath=None)
Load a Python module from it's path.
source code
list(str)
modpath_from_file(filename, extrapath=None)
given a file path return the corresponding splitted module's name (i.e name of a module or package splitted on '.')
source code
str or None
file_from_modpath(modpath, path=None, context_file=None)
given a mod path (i.e. splitted module / package name), return the corresponding file, giving priority to source file over precompiled file if it exists
source code
str or None
get_module_part(dotted_name, context_file=None)
given a dotted name return the module part of the name :
source code
list
get_modules(package, src_directory, blacklist=STD_BLACKLIST)
given a package directory return a list of all available python modules in the package and its subpackages
source code
list
get_module_files(src_directory, blacklist=STD_BLACKLIST)
given a package directory return a list of all available python module's files in the package and its subpackages
source code
str
get_source_file(filename, include_no_ext=False)
given a python module's file name return the matching source file name (the filename will be returned identically if it's a already an absolute path to a python source file...)
source code
 
cleanup_sys_modules(directories)
remove submodules of directories from sys.modules
source code
 
is_python_source(filename)
rtype: bool return: True if the filename is a python source file
source code
bool
is_standard_module(modname, std_path=(STD_LIB_DIR,))
try to guess if a module is a standard python module (by default, see std_path parameter's description)
source code
bool
is_relative(modname, from_file)
return true if the given module name is relative to the given file name
source code
Variables
  zipimport = None
hash(x)
  ZIPFILE = object()
tuple(str) PY_SOURCE_EXTS = 'py',
list of possible python source file extension
  PY_COMPILED_EXTS = 'so',
str STD_LIB_DIR = '//'
directory where standard modules are located
  EXT_LIB_DIR = get_python_lib()
dict BUILTIN_MODULES = dict.fromkeys(sys.builtin_module_names, True)
dictionary with builtin module names as key
  pkg_resources = None
hash(x)
Function Details

load_module_from_name(dotted_name, path=None, use_sys=True)

source code 
Load a Python module from its name.
Parameters:
  • dotted_name (str) - python name of a module or package
  • path (list or None) - optional list of path where the module or package should be searched (use sys.path if nothing or None is given)
  • use_sys (bool) - boolean indicating whether the sys.modules dictionary should be used or not
Returns: module
the loaded module
Raises:
  • ImportError - if the module or package is not found

load_module_from_modpath(parts, path=None, use_sys=True)

source code 
Load a python module from its splitted name.
Parameters:
  • parts (list(str) or tuple(str)) - python name of a module or package splitted on '.'
  • path (list or None) - optional list of path where the module or package should be searched (use sys.path if nothing or None is given)
  • use_sys (bool) - boolean indicating whether the sys.modules dictionary should be used or not
Returns: module
the loaded module
Raises:
  • ImportError - if the module or package is not found

load_module_from_file(filepath, path=None, use_sys=True, extrapath=None)

source code 
Load a Python module from it's path.
Parameters:
  • filepath (str) - path to the python module or package
  • path (list or None) - optional list of path where the module or package should be searched (use sys.path if nothing or None is given)
  • use_sys (bool) - boolean indicating whether the sys.modules dictionary should be used or not
Returns: module
the loaded module
Raises:
  • ImportError - if the module or package is not found

modpath_from_file(filename, extrapath=None)

source code 
given a file path return the corresponding splitted module's name (i.e name of a module or package splitted on '.')
Parameters:
  • filename (str) - file's path for which we want the module's name
  • extrapath (dict) - optional extra search path, with path as key and package name for the path as value. This is usually useful to handle package splitted in multiple directories using __path__ trick.
Returns: list(str)
the corresponding splitted module's name
Raises:
  • ImportError - if the corresponding module's name has not been found

file_from_modpath(modpath, path=None, context_file=None)

source code 
given a mod path (i.e. splitted module / package name), return the corresponding file, giving priority to source file over precompiled file if it exists
Parameters:
  • modpath (list or tuple) - splitted module's name (i.e name of a module or package splitted on '.') (this means explicit relative imports that start with dots have empty strings in this list!)
  • path (list or None) - optional list of path where the module or package should be searched (use sys.path if nothing or None is given)
  • context_file (str or None) - context file to consider, necessary if the identifier has been introduced using a relative import unresolvable in the actual context (i.e. modutils)
Returns: str or None
the path to the module's file or None if it's an integrated builtin module such as 'sys'
Raises:
  • ImportError - if there is no such module in the directory

get_module_part(dotted_name, context_file=None)

source code 

given a dotted name return the module part of the name :

>>> get_module_part('logilab.common.modutils.get_module_part')
'logilab.common.modutils'

XXX: deprecated, since it doesn't handle package precedence over module (see #10066)

Parameters:
  • dotted_name (str) - full name of the identifier we are interested in
  • context_file (str or None) - context file to consider, necessary if the identifier has been introduced using a relative import unresolvable in the actual context (i.e. modutils)
Returns: str or None
the module part of the name or None if we have not been able at all to import the given name
Raises:
  • ImportError - if there is no such module in the directory

get_modules(package, src_directory, blacklist=STD_BLACKLIST)

source code 
given a package directory return a list of all available python modules in the package and its subpackages
Parameters:
  • package (str) - the python name for the package
  • src_directory (str) - path of the directory corresponding to the package
  • blacklist (list or tuple) - optional list of files or directory to ignore, default to the value of logilab.common.STD_BLACKLIST
Returns: list
the list of all available python modules in the package and its subpackages

get_module_files(src_directory, blacklist=STD_BLACKLIST)

source code 
given a package directory return a list of all available python module's files in the package and its subpackages
Parameters:
  • src_directory (str) - path of the directory corresponding to the package
  • blacklist (list or tuple) - optional list of files or directory to ignore, default to the value of logilab.common.STD_BLACKLIST
Returns: list
the list of all available python module's files in the package and its subpackages

get_source_file(filename, include_no_ext=False)

source code 
given a python module's file name return the matching source file name (the filename will be returned identically if it's a already an absolute path to a python source file...)
Parameters:
  • filename (str) - python module's file name
Returns: str
the absolute path of the source file if it exists
Raises:
  • NoSourceFile - if no source file exists on the file system

is_standard_module(modname, std_path=(STD_LIB_DIR,))

source code 

try to guess if a module is a standard python module (by default, see std_path parameter's description)

Note: this function is known to return wrong values when inside virtualenv. See https://www.logilab.org/ticket/294756.

Parameters:
  • modname (str) - name of the module we are interested in
  • std_path (list(str) or tuple(str)) - list of path considered as standard
Returns: bool
true if the module: - is located on the path listed in one of the directory in std_path - is a built-in module

is_relative(modname, from_file)

source code 
return true if the given module name is relative to the given file name
Parameters:
  • modname (str) - name of the module we are interested in
  • from_file (str) - path of the module from which modname has been imported
Returns: bool
true if the module has been imported relatively to from_file