function test_imports_v1
A diagnostic function that tests whether all required Python modules and custom components can be successfully imported, providing visual feedback for each import attempt.
/tf/active/vicechatdev/e-ink-llm/test_hybrid_mode.py
6 - 52
simple
Purpose
This function serves as a pre-flight check or diagnostic tool to verify that all dependencies are properly installed and accessible before running the main application. It systematically attempts to import standard libraries (matplotlib, numpy, networkx) and custom modules (graphics_generator, hybrid_response_handler, hybrid_pdf_generator), printing success or failure messages with emoji indicators for easy visual scanning. Returns a boolean indicating whether all imports succeeded, making it useful for setup validation, troubleshooting dependency issues, or as part of a test suite.
Source Code
def test_imports():
"""Test if all required modules can be imported"""
print("🔍 Testing module imports...")
try:
import matplotlib.pyplot as plt
print("✅ matplotlib imported successfully")
except ImportError as e:
print(f"❌ matplotlib import failed: {e}")
return False
try:
import numpy as np
print("✅ numpy imported successfully")
except ImportError as e:
print(f"❌ numpy import failed: {e}")
return False
try:
import networkx as nx
print("✅ networkx imported successfully")
except ImportError as e:
print(f"❌ networkx import failed: {e}")
return False
try:
from graphics_generator import GraphicsGenerator, GraphicSpec, GraphicType
print("✅ graphics_generator imported successfully")
except ImportError as e:
print(f"❌ graphics_generator import failed: {e}")
return False
try:
from hybrid_response_handler import HybridResponseHandler
print("✅ hybrid_response_handler imported successfully")
except ImportError as e:
print(f"❌ hybrid_response_handler import failed: {e}")
return False
try:
from hybrid_pdf_generator import HybridPDFGenerator
print("✅ hybrid_pdf_generator imported successfully")
except ImportError as e:
print(f"❌ hybrid_pdf_generator import failed: {e}")
return False
return True
Return Value
Returns a boolean value: True if all required modules (matplotlib, numpy, networkx, graphics_generator, hybrid_response_handler, hybrid_pdf_generator) were successfully imported; False if any import failed. The function returns False immediately upon encountering the first import failure without testing remaining imports.
Dependencies
matplotlibnumpynetworkxgraphics_generatorhybrid_response_handlerhybrid_pdf_generator
Required Imports
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
from graphics_generator import GraphicsGenerator, GraphicSpec, GraphicType
from hybrid_response_handler import HybridResponseHandler
from hybrid_pdf_generator import HybridPDFGenerator
Usage Example
# Run the import test before starting main application
if test_imports():
print("All dependencies are available. Starting application...")
# Proceed with main application logic
from graphics_generator import GraphicsGenerator
generator = GraphicsGenerator()
# ... rest of application
else:
print("Import test failed. Please install missing dependencies.")
print("Run: pip install matplotlib numpy networkx")
exit(1)
Best Practices
- Run this function at application startup or as part of a test suite to catch missing dependencies early
- The function returns False on the first failure, so check console output to see which specific import failed
- Consider wrapping this in a main guard (if __name__ == '__main__':) if using as a standalone diagnostic script
- For production use, consider logging import failures to a file instead of just printing to console
- This function has side effects (prints to console), so it's primarily intended for development/debugging rather than production code paths
- The function tests imports in a specific order; if early imports fail, later ones won't be tested
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_imports 80.6% similar
-
function check_dependencies_v1 68.4% similar
-
function test_pyodbc_import 66.7% similar
-
function test_basic_functionality 55.7% similar
-
function check_dependencies 54.6% similar