Skip to content

Test Module Import

Since testcontainers-python v4.7.0

Introduction

The Testcontainers module for testing Python module imports and package management. This module provides a containerized environment for testing various aspects of Python module imports, including:

  • Basic module and package imports
  • Module reloading
  • Version-specific imports
  • Dependencies and environment variables
  • Advanced features like custom loaders and namespace packages

Adding this module to your project dependencies

Please run the following command to add the Test Module Import module to your python dependencies:

pip install testcontainers[test_module_import]

Usage examples

The module provides several examples demonstrating different use cases:

Basic Module Imports

This example demonstrates the fundamental capabilities of the TestModuleImportContainer:

  • Importing a basic Python module and accessing its attributes
  • Importing and using submodules
  • Importing and working with packages
  • Proper cleanup of imported modules
import sys

from pathlib import Path



from testcontainers.test_module_import import TestModuleImportContainer





def test_module_import():

    try:

        import test_module



        print("\nSuccessfully imported test_module")

        print(f"Module version: {test_module.__version__}")

        print(f"Module description: {test_module.__description__}")

    except ImportError as e:

        print(f"\nFailed to import test_module: {e}")





def test_submodule_import():

    try:

        from test_module import submodule



        print("\nSuccessfully imported test_module.submodule")

        print(f"Submodule function result: {submodule.test_function()}")

    except ImportError as e:

        print(f"\nFailed to import test_module.submodule: {e}")





def test_package_import():

    try:

        import test_package



        print("\nSuccessfully imported test_package")

        print(f"Package version: {test_package.__version__}")

    except ImportError as e:

        print(f"\nFailed to import test_package: {e}")





def basic_example():

    with TestModuleImportContainer():

        # Add test module to Python path

        sys.path.append(str(Path(__file__).parent))

        print("Added test module to Python path")



        # Test various imports

        test_module_import()

        test_submodule_import()

        test_package_import()



        # Clean up

        if "test_module" in sys.modules:

            del sys.modules["test_module"]

        if "test_package" in sys.modules:

            del sys.modules["test_package"]

        print("\nCleaned up imported modules")





if __name__ == "__main__":

    basic_example()

Module Reloading

This example shows how to work with module reloading functionality:

  • Importing a module and accessing its initial state
  • Reloading the module to pick up changes
  • Handling reloading errors gracefully
  • Managing module state during reloads
import importlib

import sys

from pathlib import Path



from testcontainers.test_module_import import TestModuleImportContainer





def test_module_reloading():

    try:

        import test_module



        print("\nSuccessfully imported test_module")

        print(f"Initial version: {test_module.__version__}")



        # Simulate module changes by reloading

        importlib.reload(test_module)

        print("\nSuccessfully reloaded test_module")

        print(f"Updated version: {test_module.__version__}")

    except ImportError as e:

        print(f"\nFailed to import test_module: {e}")

    except NameError:

        print("\nCould not reload test_module (not imported)")





def reloading_example():

    with TestModuleImportContainer():

        # Add test module to Python path

        sys.path.append(str(Path(__file__).parent))

        print("Added test module to Python path")



        # Test module reloading

        test_module_reloading()



        # Clean up

        if "test_module" in sys.modules:

            del sys.modules["test_module"]

        print("\nCleaned up imported modules")





if __name__ == "__main__":

    reloading_example()

Version-Specific Imports

This example demonstrates handling version-specific module imports:

  • Importing specific versions of modules
  • Managing version compatibility
  • Accessing and verifying version information
  • Working with version-specific features
import sys

from pathlib import Path



from testcontainers.test_module_import import TestModuleImportContainer





def test_version_import():

    try:

        import test_module_v2



        print("\nSuccessfully imported test_module_v2")

        print(f"Module version: {test_module_v2.__version__}")

        print(f"Module features: {test_module_v2.FEATURES}")

    except ImportError as e:

        print(f"\nFailed to import test_module_v2: {e}")





def version_example():

    with TestModuleImportContainer():

        # Add test module to Python path

        sys.path.append(str(Path(__file__).parent))

        print("Added test module to Python path")



        # Test version-specific imports

        test_version_import()



        # Clean up

        if "test_module_v2" in sys.modules:

            del sys.modules["test_module_v2"]

        print("\nCleaned up imported modules")





if __name__ == "__main__":

    version_example()

Dependencies and Environment Variables

This example shows how to handle module dependencies and environment requirements:

  • Importing modules with external dependencies
  • Managing required dependency versions
  • Setting up and accessing environment variables
  • Handling environment-specific configurations
import sys

from pathlib import Path



from testcontainers.test_module_import import TestModuleImportContainer





def test_deps_import():

    try:

        import test_module_with_deps



        print("\nSuccessfully imported test_module_with_deps")

        print(f"Dependencies: {test_module_with_deps.DEPENDENCIES}")

        print(f"Required versions: {test_module_with_deps.REQUIRED_VERSIONS}")

    except ImportError as e:

        print(f"\nFailed to import test_module_with_deps: {e}")





def test_env_import():

    try:

        import test_module_with_env



        print("\nSuccessfully imported test_module_with_env")

        print(f"Environment variables: {test_module_with_env.ENV_VARS}")

        print(f"Environment values: {test_module_with_env.ENV_VALUES}")

    except ImportError as e:

        print(f"\nFailed to import test_module_with_env: {e}")





def deps_and_env_example():

    with TestModuleImportContainer():

        # Add test module to Python path

        sys.path.append(str(Path(__file__).parent))

        print("Added test module to Python path")



        # Test dependencies and environment imports

        test_deps_import()

        test_env_import()



        # Clean up

        if "test_module_with_deps" in sys.modules:

            del sys.modules["test_module_with_deps"]

        if "test_module_with_env" in sys.modules:

            del sys.modules["test_module_with_env"]

        print("\nCleaned up imported modules")





if __name__ == "__main__":

    deps_and_env_example()

Advanced Features

This example demonstrates advanced module import capabilities:

  • Using custom module loaders for specialized import scenarios
  • Working with namespace packages
  • Managing entry points
  • Handling complex module configurations
import sys

from pathlib import Path



from testcontainers.test_module_import import TestModuleImportContainer





def test_custom_loader_import():

    try:

        import test_module_custom_loader



        print("\nSuccessfully imported test_module_custom_loader")

        print(f"Loader type: {test_module_custom_loader.LOADER_TYPE}")

        print(f"Loader configuration: {test_module_custom_loader.LOADER_CONFIG}")

    except ImportError as e:

        print(f"\nFailed to import test_module_custom_loader: {e}")





def test_namespace_import():

    try:

        import test_namespace_package



        print("\nSuccessfully imported test_namespace_package")

        print(f"Namespace: {test_namespace_package.__namespace__}")

        print(f"Available subpackages: {test_namespace_package.SUBPACKAGES}")

    except ImportError as e:

        print(f"\nFailed to import test_namespace_package: {e}")





def test_entry_points_import():

    try:

        import test_module_with_entry_points



        print("\nSuccessfully imported test_module_with_entry_points")

        print(f"Entry points: {test_module_with_entry_points.ENTRY_POINTS}")

        print(f"Entry point groups: {test_module_with_entry_points.ENTRY_POINT_GROUPS}")

    except ImportError as e:

        print(f"\nFailed to import test_module_with_entry_points: {e}")





def advanced_features_example():

    with TestModuleImportContainer():

        # Add test module to Python path

        sys.path.append(str(Path(__file__).parent))

        print("Added test module to Python path")



        # Test advanced features

        test_custom_loader_import()

        test_namespace_import()

        test_entry_points_import()



        # Clean up

        for module in ["test_module_custom_loader", "test_namespace_package", "test_module_with_entry_points"]:

            if module in sys.modules:

                del sys.modules[module]

        print("\nCleaned up imported modules")





if __name__ == "__main__":

    advanced_features_example()