diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index dac815b..cdfe8b7 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -68,7 +68,35 @@ pub struct AudioTrack { async fn execute_python_command(args: &[String]) -> Result { println!("Executing Python command with args: {:?}", args); - let mut cmd = Command::new("python3"); + // Get the current working directory and move up one level from src-tauri to project root + let current_dir = std::env::current_dir() + .map_err(|e| format!("Failed to get current directory: {}", e))?; + + let project_root = if current_dir.ends_with("src-tauri") { + current_dir.parent().unwrap_or(¤t_dir).to_path_buf() + } else { + current_dir + }; + + println!("Current directory: {:?}", std::env::current_dir().unwrap_or_default()); + println!("Project root: {:?}", project_root); + + // Verify that the Python script exists + let script_path = project_root.join(&args[0]); + if !script_path.exists() { + return Err(format!("Python script not found: {:?}", script_path)); + } + println!("Python script found: {:?}", script_path); + + // Try python3 first, then python (for Windows compatibility) + let python_cmd = if cfg!(target_os = "windows") { + "python" + } else { + "python3" + }; + + let mut cmd = Command::new(python_cmd); + cmd.current_dir(&project_root); // Set working directory to project root for arg in args { cmd.arg(arg); @@ -77,7 +105,7 @@ async fn execute_python_command(args: &[String]) -> Result { let output = cmd .output() .map_err(|e| { - let error_msg = format!("Failed to execute Python script: {} (args: {:?})", e, args); + let error_msg = format!("Failed to execute Python script: {} (args: {:?}, cwd: {:?})", e, args, project_root); println!("Command execution failed: {}", error_msg); error_msg })?; @@ -288,11 +316,31 @@ pub async fn batch_generate_ai_videos(request: BatchAIVideoRequest) -> Result Result { println!("Testing AI video environment..."); + // Get project root directory + let current_dir = std::env::current_dir() + .map_err(|e| format!("Failed to get current directory: {}", e))?; + + let project_root = if current_dir.ends_with("src-tauri") { + current_dir.parent().unwrap_or(¤t_dir).to_path_buf() + } else { + current_dir + }; + + println!("Testing from project root: {:?}", project_root); + + // Try python3 first, then python (for Windows compatibility) + let python_cmd = if cfg!(target_os = "windows") { + "python" + } else { + "python3" + }; + // Test 1: Python availability - let python_test = Command::new("python3") + let python_test = Command::new(python_cmd) .arg("--version") + .current_dir(&project_root) .output() - .map_err(|e| format!("Python3 not found: {}", e))?; + .map_err(|e| format!("Python not found: {}", e))?; if !python_test.status.success() { return Err("Python3 is not available".to_string()); @@ -302,9 +350,10 @@ pub async fn test_ai_video_environment() -> Result { println!("Python version: {}", python_version); // Test 2: Check if AI video module can be imported - let module_test = Command::new("python3") + let module_test = Command::new(python_cmd) .arg("-c") .arg("import sys; sys.path.append('python_core'); from ai_video import VideoGenerator; print('AI video module imported successfully')") + .current_dir(&project_root) .output() .map_err(|e| format!("Failed to test module import: {}", e))?; diff --git a/test_path_fix.py b/test_path_fix.py new file mode 100644 index 0000000..a30d504 --- /dev/null +++ b/test_path_fix.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +""" +Test script to verify path fixes for AI video generation +""" + +import os +import sys +import json + +def test_environment(): + """Test the environment and paths""" + result = { + "status": "success", + "current_directory": os.getcwd(), + "python_executable": sys.executable, + "python_version": sys.version, + "script_path": __file__, + "python_core_exists": os.path.exists("python_core"), + "ai_video_module_exists": os.path.exists("python_core/ai_video"), + "video_generator_exists": os.path.exists("python_core/ai_video/video_generator.py") + } + + # Test module import + try: + sys.path.append('python_core') + from ai_video import VideoGenerator + result["module_import"] = "success" + result["module_import_error"] = None + except Exception as e: + result["module_import"] = "failed" + result["module_import_error"] = str(e) + + return result + +if __name__ == "__main__": + result = test_environment() + print(json.dumps(result, indent=2, ensure_ascii=False))