function check_dependencies
Validates the installation status of all required Python packages for the DocChat application by attempting to import each dependency and logging the results.
/tf/active/vicechatdev/docchat/integration.py
62 - 105
simple
Purpose
This function performs a dependency check for the DocChat application to ensure all required packages are installed before the application runs. It verifies the presence of packages needed for document processing (pypdf, python-docx), vector databases (chromadb), LLM integrations (langchain variants), and embeddings (sentence_transformers). The function provides detailed feedback about missing packages and installation instructions, making it useful for deployment validation and troubleshooting.
Source Code
def check_dependencies(app):
"""
Check if all required DocChat dependencies are installed.
Args:
app: Flask application instance
Returns:
dict: Status information about dependencies
"""
required_packages = [
'chromadb',
'langchain',
'langchain_openai',
'langchain_anthropic',
'langchain_google_genai',
'sentence_transformers',
'pypdf',
'python-docx'
]
results = {
'all_installed': True,
'packages': {}
}
for package in required_packages:
try:
__import__(package.replace('-', '_'))
results['packages'][package] = 'installed'
except ImportError:
results['packages'][package] = 'missing'
results['all_installed'] = False
if not results['all_installed']:
logger.warning("⚠️ Some DocChat dependencies are missing:")
for pkg, status in results['packages'].items():
if status == 'missing':
logger.warning(f" - {pkg}")
logger.warning(" Install with: pip install -r docchat/requirements.txt")
else:
logger.info("✅ All DocChat dependencies are installed")
return results
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
app |
- | - | positional_or_keyword |
Parameter Details
app: Flask application instance. While passed as a parameter, it is not actually used in the function body. This parameter likely exists for consistency with other initialization functions or for potential future use in accessing application configuration or logging settings.
Return Value
Returns a dictionary with two keys: 'all_installed' (boolean indicating whether all dependencies are present) and 'packages' (dictionary mapping each package name to its status string, either 'installed' or 'missing'). Example: {'all_installed': False, 'packages': {'chromadb': 'installed', 'langchain': 'missing', ...}}
Dependencies
chromadblangchainlangchain_openailangchain_anthropiclangchain_google_genaisentence_transformerspypdfpython-docxlogging
Required Imports
import logging
Conditional/Optional Imports
These imports are only needed under specific conditions:
import chromadb
Condition: checked by the function but not imported at module level
Required (conditional)import langchain
Condition: checked by the function but not imported at module level
Required (conditional)import langchain_openai
Condition: checked by the function but not imported at module level
Required (conditional)import langchain_anthropic
Condition: checked by the function but not imported at module level
Required (conditional)import langchain_google_genai
Condition: checked by the function but not imported at module level
Required (conditional)import sentence_transformers
Condition: checked by the function but not imported at module level
Required (conditional)import pypdf
Condition: checked by the function but not imported at module level
Required (conditional)import docx
Condition: checked by the function but not imported at module level (package name is 'python-docx' but imports as 'docx')
Required (conditional)Usage Example
from flask import Flask
import logging
# Setup logger
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
# Create Flask app
app = Flask(__name__)
# Check dependencies
results = check_dependencies(app)
if results['all_installed']:
print("All dependencies are installed!")
else:
print("Missing packages:")
for pkg, status in results['packages'].items():
if status == 'missing':
print(f" - {pkg}")
# Access individual package status
if results['packages']['chromadb'] == 'installed':
print("ChromaDB is available")
Best Practices
- Ensure a logger is configured before calling this function, as it relies on a module-level 'logger' variable
- Call this function during application initialization or startup to catch missing dependencies early
- Note that the 'app' parameter is currently unused; consider removing it or utilizing it for accessing app-specific configuration
- The function converts package names with hyphens to underscores for import (e.g., 'python-docx' becomes 'python_docx'), which is correct Python behavior
- This function only checks if packages can be imported, not their versions; consider adding version validation if specific versions are required
- The function uses __import__() which is appropriate for dynamic imports but doesn't validate package functionality
- Consider wrapping the function call in a try-except block to handle unexpected import errors gracefully
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_integration_status 73.8% similar
-
function check_dependencies_v1 73.1% similar
-
function integrate_docchat 68.7% similar
-
function register_docchat 59.0% similar
-
function configure_docchat 55.0% similar