module documentation
(source)

Python modules manipulation utility functions.
Class ​No​Source​File exception raised when we are not able to get a python source file for a precompiled file
Function check​_modpath​_has​_init check there are some __init__.py all along the way
Function file​_from​_modpath Undocumented
Function file​_info​_from​_modpath given a mod path (i.e. split module / package name), return the corresponding file, giving priority to source file over precompiled file if it exists
Function get​_module​_files given a package directory return a list of all available python module's files in the package and its subpackages
Function get​_module​_part given a dotted name return the module part of the name :
Function get​_source​_file given a python module's file name return the matching source file name (the filename will be returned identically if it's already an absolute path to a python source file...)
Function is​_directory Undocumented
Function is​_module​_name​_part​_of​_extension​_package​_whitelist Returns True if one part of the module name is in the package whitelist
Function is​_namespace Undocumented
Function is​_python​_source rtype: bool return: True if the filename is a python source file
Function is​_relative return true if the given module name is relative to the given file name
Function is​_standard​_module try to guess if a module is a standard python module (by default, see std_path parameter's description)
Function load​_module​_from​_file Load a Python module from it's path.
Function load​_module​_from​_modpath Load a python module from its split name.
Function load​_module​_from​_name Load a Python module from its name.
Function modpath​_from​_file Get the corresponding split module's name from a filename
Function modpath​_from​_file​_with​_callback Undocumented
Constant BUILTIN​_MODULES dictionary with builtin module names has key
Constant EXT​_LIB​_DIRS Undocumented
Constant IS​_JYTHON Undocumented
Constant PY​_COMPILED​_EXTS Undocumented
Constant PY​_SOURCE​_EXTS list of possible python source file extension
Constant STD​_LIB​_DIRS directories where standard modules are located
Function _cache​_normalize​_path Normalize path with caching.
Function _get​_relative​_base​_path Extracts the relative mod path of the file to import from
Function _handle​_blacklist remove files/directories in the black list
Function _has​_init if the given directory has a valid __init__ file, return its path, else return None
Function _is​_python​_file return true if the given filename should be considered as a python file
Function _normalize​_path Resolve symlinks in path and convert to absolute path.
Function _path​_from​_filename Undocumented
Function _posix​_path Undocumented
Function _spec​_from​_modpath given a mod path (i.e. split module / package name), return the corresponding spec
Constant _NORM​_PATH​_CACHE Undocumented
Variable _root Undocumented
def check_modpath_has_init(path, mod_path): (source)
check there are some __init__.py all along the way
def file_from_modpath(modpath, path=None, context_file=None): (source)

Undocumented

def file_info_from_modpath(modpath, path=None, context_file=None): (source)
given a mod path (i.e. split module / package name), return the corresponding file, giving priority to source file over precompiled file if it exists
Parameters
modpath:list or tuplesplit module's name (i.e name of a module or package split on '.') (this means explicit relative imports that start with dots have empty strings in this list!)
path:list or Noneoptional list of path where the module or package should be searched (use sys.path if nothing or None is given)
context​_file:str or Nonecontext 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, import type)the path to the module's file or None if it's an integrated builtin module such as 'sys'
Raises
ImportErrorif there is no such module in the directory
def get_module_files(src_directory, blacklist, list_all=False): (source)
given a package directory return a list of all available python module's files in the package and its subpackages
Parameters
src​_directory:strpath of the directory corresponding to the package
blacklist:list or tupleiterable list of files or directories to ignore.
list​_all:boolget files from all paths, including ones without __init__.py
Returns
listthe list of all available python module's files in the package and its subpackages
def get_module_part(dotted_name, context_file=None): (source)

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

>>> get_module_part('astroid.as_string.dump')
'astroid.as_string'

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

Parameters
dotted​_name:strfull name of the identifier we are interested in
context​_file:str or Nonecontext 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 Nonethe module part of the name or None if we have not been able at all to import the given name
Raises
ImportErrorif there is no such module in the directory
def get_source_file(filename, include_no_ext=False): (source)
given a python module's file name return the matching source file name (the filename will be returned identically if it's already an absolute path to a python source file...)
Parameters
filename:strpython module's file name
include​_no​_extUndocumented
Returns
strthe absolute path of the source file if it exists
Raises
NoSourceFileif no source file exists on the file system
def is_directory(specobj): (source)

Undocumented

def is_module_name_part_of_extension_package_whitelist(module_name, package_whitelist): (source)

Returns True if one part of the module name is in the package whitelist

>>> is_module_name_part_of_extension_package_whitelist('numpy.core.umath', {'numpy'})
True
Parameters
module​_name:strUndocumented
package​_whitelist:Set[str]Undocumented
Returns
boolUndocumented
def is_namespace(specobj): (source)

Undocumented

def is_python_source(filename): (source)
rtype: bool return: True if the filename is a python source file
def is_relative(modname, from_file): (source)
return true if the given module name is relative to the given file name
Parameters
modname:strname of the module we are interested in
from​_file:strpath of the module from which modname has been imported
Returns
booltrue if the module has been imported relatively to from_file
def is_standard_module(modname, std_path=None): (source)
try to guess if a module is a standard python module (by default, see std_path parameter's description)
Parameters
modname:strname of the module we are interested in
std​_path:list(str) or tuple(str)list of path considered has standard
Returns
booltrue if the module: - is located on the path listed in one of the directory in std_path - is a built-in module
def load_module_from_file(filepath): (source)
Load a Python module from it's path.
Parameters
filepath:strpath to the python module or package
Returns
modulethe loaded module
Raises
ImportErrorif the module or package is not found
def load_module_from_modpath(parts): (source)
Load a python module from its split name.
Parameters
parts:list(str) or tuple(str)python name of a module or package split on '.'
Returns
modulethe loaded module
Raises
ImportErrorif the module or package is not found
def load_module_from_name(dotted_name): (source)
Load a Python module from its name.
Parameters
dotted​_name:strpython name of a module or package
Returns
modulethe loaded module
Raises
ImportErrorif the module or package is not found
def modpath_from_file(filename, path=None): (source)

Get the corresponding split module's name from a filename

This function will return the name of a module or package split on ..

Parameters
filename:strfile's path for which we want the module's name
pathUndocumented
​Optional[​List[str]] path:Optional list of path where the module or package should be searched (use sys.path if nothing or None is given)Undocumented
Returns
list(str)the corresponding split module's name
Raises
ImportErrorif the corresponding module's name has not been found
def modpath_from_file_with_callback(filename, path=None, is_package_cb=None): (source)

Undocumented

BUILTIN_MODULES: dict = (source)
dictionary with builtin module names has key
Value
dict.fromkeys(sys.builtin_module_names, True)
EXT_LIB_DIRS = (source)

Undocumented

Value
set([get_python_lib(), get_python_lib(True)])
IS_JYTHON = (source)

Undocumented

Value
(platform.python_implementation() == 'Jython')
PY_COMPILED_EXTS: tuple[str, ...] = (source)

Undocumented

Value
('dll', 'pyd')
PY_SOURCE_EXTS: tuple(str) = (source)
list of possible python source file extension
Value
('py', 'pyw')
STD_LIB_DIRS: set of str = (source)
directories where standard modules are located
Value
set()
def _cache_normalize_path(path): (source)
Normalize path with caching.
Parameters
path:strUndocumented
Returns
strUndocumented
def _get_relative_base_path(filename, path_to_check): (source)

Extracts the relative mod path of the file to import from

Check if a file is within the passed in path and if so, returns the relative mod path from the one passed in.

If the filename is no in path_to_check, returns None

Note this function will look for both abs and realpath of the file, this allows to find the relative base path even if the file is a symlink of a file in the passed in path

Examples:
_get_relative_base_path("/a/b/c/d.py", "/a/b") -> ["c","d"] _get_relative_base_path("/a/b/c/d.py", "/dev") -> None
def _handle_blacklist(blacklist, dirnames, filenames): (source)

remove files/directories in the black list

dirnames/filenames are usually from os.walk

def _has_init(directory): (source)
if the given directory has a valid __init__ file, return its path, else return None
def _is_python_file(filename): (source)

return true if the given filename should be considered as a python file

.pyc and .pyo are ignored

def _normalize_path(path): (source)

Resolve symlinks in path and convert to absolute path.

Note that environment variables and ~ in the path need to be expanded in advance.

This can be cached by using _cache_normalize_path.

Parameters
path:strUndocumented
Returns
strUndocumented
def _path_from_filename(filename, is_jython=IS_JYTHON): (source)

Undocumented

def _posix_path(path): (source)

Undocumented

def _spec_from_modpath(modpath, path=None, context=None): (source)

given a mod path (i.e. split module / package name), return the corresponding spec

this function is used internally, see file_from_modpath's documentation for more information

_NORM_PATH_CACHE: Dict[str, str] = (source)

Undocumented

Value
{}
_root = (source)

Undocumented