diff --git a/modules/agentCoder.py b/modules/agentCoder.py index a263c68e..9e5b4d97 100644 --- a/modules/agentCoder.py +++ b/modules/agentCoder.py @@ -605,11 +605,67 @@ Return ONLY Python code without explanations or markdown. # Create venv logger.debug(f"Creating virtual environment at {venvPath}") - subprocess.run([sys.executable, "-m", "venv", venvPath], - check=True, capture_output=True) - # Get Python executable path - pythonExe = os.path.join(venvPath, "Scripts", "python.exe") if os.name == 'nt' else os.path.join(venvPath, "bin", "python") + try: + # First try with sys.executable - the standard approach + subprocess.run([sys.executable, "-m", "venv", venvPath], + check=True, capture_output=True, timeout=60) + logger.debug("Virtual environment created successfully with sys.executable") + except (subprocess.SubprocessError, subprocess.CalledProcessError) as e: + logger.warning(f"Failed to create venv with sys.executable: {str(e)}") + + # Fallback method 1: Try with explicit 'python3' command + try: + logger.debug("Trying to create virtual environment with python3 command") + subprocess.run(["python3", "-m", "venv", venvPath], + check=True, capture_output=True, timeout=60) + logger.debug("Virtual environment created successfully with python3") + except (subprocess.SubprocessError, subprocess.CalledProcessError) as e: + logger.warning(f"Failed to create venv with python3: {str(e)}") + + # Fallback method 2: Try with virtualenv instead of venv + try: + logger.debug("Trying to create virtual environment with virtualenv module") + subprocess.run([sys.executable, "-m", "pip", "install", "virtualenv"], + check=False, capture_output=True, timeout=60) + subprocess.run([sys.executable, "-m", "virtualenv", venvPath], + check=True, capture_output=True, timeout=60) + logger.debug("Virtual environment created successfully with virtualenv") + except (subprocess.SubprocessError, subprocess.CalledProcessError) as e: + # If all methods fail, raise an exception + error_msg = f"Failed to create virtual environment with all methods: {str(e)}" + logger.error(error_msg) + raise RuntimeError(error_msg) + + # Get Python executable path - adjusted for OS + if os.name == 'nt': # Windows + pythonExe = os.path.join(venvPath, "Scripts", "python.exe") + else: # Linux/Mac + pythonExe = os.path.join(venvPath, "bin", "python") + + # Verify python executable exists + if not os.path.exists(pythonExe): + # Try to find it + if os.name == 'nt': + possible_paths = [ + os.path.join(venvPath, "Scripts", "python.exe"), + os.path.join(venvPath, "Scripts", "python") + ] + else: + possible_paths = [ + os.path.join(venvPath, "bin", "python"), + os.path.join(venvPath, "bin", "python3") + ] + + for path in possible_paths: + if os.path.exists(path): + pythonExe = path + logger.debug(f"Found Python executable at: {pythonExe}") + break + + if not os.path.exists(pythonExe): + logger.error(f"Python executable not found at expected path: {pythonExe}") + raise FileNotFoundError(f"Python executable not found in virtual environment") # 2. Install requirements if provided if requirements: