module documentation

Python modules manipulation utility functions. :type PY_SOURCE_EXTS: tuple(str) :var PY_SOURCE_EXTS: list of possible python source file extension :type STD_LIB_DIRS: set of str :var STD_LIB_DIRS: directories where standard modules are located :type BUILTIN_MODULES: dict :var BUILTIN_MODULES: dictionary with builtin module names has key

Exception NoSourceFile 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.
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.
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 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 is_stdlib_module Return: True if the modname is in the standard library
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
Function module_in_path Try to determine if a module is imported from one of the specified paths
Constant BUILTIN_MODULES Undocumented
Constant EXT_LIB_DIRS Undocumented
Constant PY_COMPILED_EXTS Undocumented
Constant PY_SOURCE_EXTS Undocumented
Constant STD_LIB_DIRS Undocumented
Variable logger Undocumented
Function _cache_normalize_path Normalize path with caching.
Function _cache_normalize_path_ Undocumented
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.
def check_modpath_has_init(path: str, mod_path: list[str]) -> bool: (source)

Check there are some __init__.py all along the way.

def file_from_modpath(modpath: list[str], path: Sequence[str]|None = None, context_file: str|None = None) -> str|None: (source)

Undocumented

def file_info_from_modpath(modpath: list[str], path: Sequence[str]|None = None, context_file: str|None = None) -> spec.ModuleSpec: (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. :param modpath: split 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!) :param path: optional list of path where the module or package should be searched (use sys.path if nothing or None is given) :param context_file: context file to consider, necessary if the identifier has been introduced using a relative import unresolvable in the actual context (i.e. modutils) :raise ImportError: if there is no such module in the directory :return: the path to the module's file or None if it's an integrated builtin module such as 'sys'

def get_module_files(src_directory: str, blacklist: Sequence[str], list_all: bool = False) -> list[str]: (source)

Given a package directory return a list of all available python module's files in the package and its subpackages. :param src_directory: path of the directory corresponding to the package :param blacklist: iterable list of files or directories to ignore. :param list_all: get files from all paths, including ones without __init__.py :return: the list of all available python module's files in the package and its subpackages

def get_module_part(dotted_name: str, context_file: str|None = None) -> str: (source)

Given a dotted name return the module part of the name : >>> get_module_part('astroid.as_string.dump') 'astroid.as_string' :param dotted_name: full name of the identifier we are interested in :param context_file: context file to consider, necessary if the identifier has been introduced using a relative import unresolvable in the actual context (i.e. modutils) :raise ImportError: if there is no such module in the directory :return: the module part of the name or None if we have not been able at all to import the given name XXX: deprecated, since it doesn't handle package precedence over module (see #10066)

def get_source_file(filename: str, include_no_ext: bool = False) -> str: (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...) :param filename: python module's file name :raise NoSourceFile: if no source file exists on the file system :return: the absolute path of the source file if it exists

def is_directory(specobj: spec.ModuleSpec) -> bool: (source)

Undocumented

def is_module_name_part_of_extension_package_whitelist(module_name: str, package_whitelist: set[str]) -> bool: (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

def is_namespace(specobj: spec.ModuleSpec) -> bool: (source)

Undocumented

def is_python_source(filename: str|None) -> bool: (source)

Return: True if the filename is a python source file.

def is_relative(modname: str, from_file: str) -> bool: (source)

Return true if the given module name is relative to the given file name. :param modname: name of the module we are interested in :param from_file: path of the module from which modname has been imported :return: true if the module has been imported relatively to `from_file`

def is_standard_module(modname: str, std_path: Iterable[str]|None = None) -> bool: (source)

Try to guess if a module is a standard python module (by default, see `std_path` parameter's description). :param modname: name of the module we are interested in :param std_path: list of path considered has standard :return: true if the module: - is located on the path listed in one of the directory in `std_path` - is a built-in module

def is_stdlib_module(modname: str) -> bool: (source)

Return: True if the modname is in the standard library

def load_module_from_file(filepath: str) -> types.ModuleType: (source)

Load a Python module from it's path. :type filepath: str :param filepath: path to the python module or package :raise ImportError: if the module or package is not found :rtype: module :return: the loaded module

def load_module_from_modpath(parts: Sequence[str]) -> types.ModuleType: (source)

Load a python module from its split name. :param parts: python name of a module or package split on '.' :raise ImportError: if the module or package is not found :return: the loaded module

def load_module_from_name(dotted_name: str) -> types.ModuleType: (source)

Load a Python module from its name. :type dotted_name: str :param dotted_name: python name of a module or package :raise ImportError: if the module or package is not found :rtype: module :return: the loaded module

def modpath_from_file(filename: str, path: Sequence[str]|None = None) -> list[str]: (source)

Get the corresponding split module's name from a filename. This function will return the name of a module or package split on `.`. :type filename: str :param filename: file's path for which we want the module's name :type 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) :raise ImportError: if the corresponding module's name has not been found :rtype: list(str) :return: the corresponding split module's name

def modpath_from_file_with_callback(filename: str, path: Sequence[str]|None = None, is_package_cb: Callable[[str, list[str]], bool]|None = None) -> list[str]: (source)

Undocumented

def module_in_path(modname: str, path: str|Iterable[str]) -> bool: (source)

Try to determine if a module is imported from one of the specified paths :param modname: name of the module :param path: paths to consider :return: true if the module: - is located on the path listed in one of the directory in `paths`

BUILTIN_MODULES = (source)
EXT_LIB_DIRS = (source)

Undocumented

Value
set([sysconfig.get_path('purelib'), sysconfig.get_path('platlib')])
PY_COMPILED_EXTS: tuple[str, ...] = (source)

Undocumented

Value
('dll', 'pyd')
PY_SOURCE_EXTS: tuple[str, ...] = (source)

Undocumented

Value
('py', 'pyw')
STD_LIB_DIRS = (source)

Undocumented

Value
set([sysconfig.get_path('stdlib'), sysconfig.get_path('platstdlib')])

Undocumented

def _cache_normalize_path(path: str) -> str: (source)

Normalize path with caching.

@lru_cache()
def _cache_normalize_path_(path: str) -> str: (source)

Undocumented

def _get_relative_base_path(filename: str, path_to_check: str) -> list[str]|None: (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: Sequence[str], dirnames: list[str], filenames: list[str]): (source)

Remove files/directories in the black list. dirnames/filenames are usually from os.walk

def _has_init(directory: str) -> str|None: (source)

If the given directory has a valid __init__ file, return its path, else return None.

def _is_python_file(filename: str) -> bool: (source)

Return true if the given filename should be considered as a python file. .pyc and .pyo are ignored

def _normalize_path(path: str) -> str: (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.

def _path_from_filename(filename: str, is_jython: bool = IS_JYTHON) -> str: (source)

Undocumented

def _posix_path(path: str) -> str: (source)

Undocumented

def _spec_from_modpath(modpath: list[str], path: Sequence[str]|None = None, context: str|None = None) -> spec.ModuleSpec: (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