Original code copyright (C) 2009-2022 Rudolf Cardinal (

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Functions to work with Python modules.

cardinal_pythonlib.modules.contains_c_extension(module: module, import_all_submodules: bool = True, include_external_imports: bool = False, seen: List[module] | None = None) bool[source]

Extends is_c_extension() by asking: is this module, or any of its submodules, a C extension?

  • module – Previously imported module object to be tested.

  • import_all_submodules – explicitly import all submodules of this module?

  • include_external_imports – check modules in other packages that this module imports?

  • seen – used internally for recursion (to deal with recursive modules); should be None when called by users


True only if this module or one of its submodules is a C extension.

Return type:



import logging
from cardinal_pythonlib.modules import contains_c_extension
from cardinal_pythonlib.logs import main_only_quicksetup_rootlogger

import _elementtree as et
import os

import arrow
import alembic
import django
import numpy
import numpy.core.multiarray as numpy_multiarray

log = logging.getLogger(__name__)
# logging.basicConfig(level=logging.DEBUG)  # be verbose

contains_c_extension(os)  # False
contains_c_extension(et)  # False

contains_c_extension(numpy)  # True -- different from is_c_extension()
contains_c_extension(numpy_multiarray)  # True

contains_c_extension(arrow)  # False

contains_c_extension(alembic)  # False
contains_c_extension(alembic, include_external_imports=True)  # True
# ... this example shows that Alembic imports hashlib, which can import
#     _hashlib, which is a C extension; however, that doesn't stop us (for
#     example) installing Alembic on a machine with no C compiler

cardinal_pythonlib.modules.import_submodules(package: str | module, base_package_for_relative_import: str | None = None, recursive: bool = True) Dict[str, module][source]

Import all submodules of a module, recursively, including subpackages.

  • package – package (name or actual module)

  • base_package_for_relative_import – path to prepend?

  • recursive – import submodules too?


mapping from full module name to module

Return type:


cardinal_pythonlib.modules.is_builtin_module(module: module) bool[source]

Is this module a built-in module, like os? Method is as per inspect.getfile().

cardinal_pythonlib.modules.is_c_extension(module: module) bool[source]

Modified from

True only if the passed module is a C extension implemented as a dynamically linked shared library specific to the current platform.


module – Previously imported module object to be tested.


True only if this module is a C extension.

Return type:



from cardinal_pythonlib.modules import is_c_extension

import os
import _elementtree as et
import numpy
import numpy.core.multiarray as numpy_multiarray

is_c_extension(os)  # False
is_c_extension(numpy)  # False
is_c_extension(et)  # False on my system (Python 3.5.6). True in the original example.
is_c_extension(numpy_multiarray)  # True