#!/usr/bin/env python """ Debug script to diagnose FastAPI application startup issues. This script checks for common issues that might prevent the app from starting. """ import importlib import os import sys import traceback from pathlib import Path # Configure the Python path current_dir = Path(__file__).parent.absolute() sys.path.insert(0, str(current_dir)) def check_import(module_name): """Try to import a module and report any errors.""" try: importlib.import_module(module_name) print(f"✅ Successfully imported {module_name}") return True except Exception as e: print(f"❌ Failed to import {module_name}: {e}") print("Traceback:") traceback.print_exc() return False def check_file_exists(file_path): """Check if a file exists.""" path = Path(file_path) if path.exists(): print(f"✅ File exists: {file_path}") return True else: print(f"❌ File does not exist: {file_path}") return False def check_directory_exists(dir_path): """Check if a directory exists and try to create it if it doesn't.""" path = Path(dir_path) if path.exists() and path.is_dir(): print(f"✅ Directory exists: {dir_path}") return True else: print(f"❌ Directory does not exist: {dir_path}") try: path.mkdir(parents=True, exist_ok=True) print(f"✅ Created directory: {dir_path}") return True except Exception as e: print(f"❌ Failed to create directory {dir_path}: {e}") return False def check_directory_writable(dir_path): """Check if a directory is writable.""" path = Path(dir_path) if not path.exists(): print(f"❌ Directory does not exist: {dir_path}") return False try: test_file = path / "write_test_file.txt" with open(test_file, "w") as f: f.write("Test write access") test_file.unlink() # Remove the test file print(f"✅ Directory is writable: {dir_path}") return True except Exception as e: print(f"❌ Directory is not writable: {dir_path}: {e}") return False def check_database(): """Check database configuration and connectivity.""" try: from app.db.session import SQLALCHEMY_DATABASE_URL, engine print(f"✅ Database URL: {SQLALCHEMY_DATABASE_URL}") # Check the database directory db_path = Path(SQLALCHEMY_DATABASE_URL.replace("sqlite:///", "")) db_dir = db_path.parent check_directory_exists(db_dir) check_directory_writable(db_dir) # Try to connect to the database try: conn = engine.connect() conn.close() print("✅ Successfully connected to the database") except Exception as e: print(f"❌ Failed to connect to the database: {e}") traceback.print_exc() except Exception as e: print(f"❌ Error checking database: {e}") traceback.print_exc() def check_app_startup(): """Try to import the FastAPI app and check for errors.""" try: # First check if we can import FastAPI import fastapi print(f"✅ FastAPI version: {fastapi.__version__}") # Then try to import the app from main import app print("✅ Successfully imported the FastAPI app") # Check app configuration print(f"✅ App title: {app.title}") print(f"✅ App version: {app.version}") print(f"✅ OpenAPI URL: {app.openapi_url}") return True except Exception as e: print(f"❌ Error during app startup: {e}") traceback.print_exc() return False def main(): """Run all checks.""" print("\n=== Python Environment ===") print(f"Python version: {sys.version}") print(f"Python executable: {sys.executable}") print(f"Current directory: {os.getcwd()}") print("\n=== Checking Critical Imports ===") check_import("fastapi") check_import("uvicorn") check_import("sqlalchemy") check_import("pydantic") check_import("alembic") print("\n=== Checking App Imports ===") check_import("app") check_import("app.api") check_import("app.core") check_import("app.db") check_import("app.models") check_import("app.schemas") print("\n=== Checking Critical Files ===") check_file_exists("main.py") check_file_exists("app/core/config.py") check_file_exists("app/db/session.py") print("\n=== Checking Storage Directories ===") project_root = Path(__file__).parent.absolute() app_storage_dir = project_root / "app" / "storage" / "db" check_directory_exists(app_storage_dir) check_directory_writable(app_storage_dir) print("\n=== Checking Database ===") check_database() print("\n=== Testing App Startup ===") check_app_startup() print("\n=== Debug Complete ===") if __name__ == "__main__": main()