list_python_environments
Discover all available Python environments, including system Python and conda environments, using this tool to manage and interact with your Python setups effectively.
Instructions
List all available Python environments (system Python and conda environments).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- mcp_python_interpreter/server.py:468-481 (handler)The main handler function decorated with @mcp.tool() that lists available Python environments by calling the helper function and formatting the output as a string.def list_python_environments() -> str: """List all available Python environments (system Python and conda environments).""" environments = get_python_environments() if not environments: return "No Python environments found." result = "Available Python Environments:\n\n" for env in environments: result += f"- Name: {env['name']}\n" result += f" Path: {env['path']}\n" result += f" Version: Python {env['version']}\n\n" return result
- Helper function that discovers available Python environments including system Python, custom default, and conda environments by running subprocess commands to query versions and list envs.def get_python_environments() -> List[Dict[str, str]]: """Get all available Python environments.""" environments = [] if DEFAULT_PYTHON_PATH != sys.executable: try: result = subprocess.run( [DEFAULT_PYTHON_PATH, "-c", "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')"], capture_output=True, text=True, check=True, timeout=10, stdin=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0 ) version = result.stdout.strip() environments.append({ "name": "default", "path": DEFAULT_PYTHON_PATH, "version": version }) except Exception as e: print(f"Error getting version for custom Python path: {e}", file=sys.stderr) environments.append({ "name": "system", "path": sys.executable, "version": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}" }) # Try conda environments try: result = subprocess.run( ["conda", "info", "--envs", "--json"], capture_output=True, text=True, check=False, timeout=10, stdin=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0 ) if result.returncode == 0: conda_info = json.loads(result.stdout) for env in conda_info.get("envs", []): env_name = os.path.basename(env) if env_name == "base": env_name = "conda-base" python_path = os.path.join(env, "bin", "python") if not os.path.exists(python_path): python_path = os.path.join(env, "python.exe") if os.path.exists(python_path): try: version_result = subprocess.run( [python_path, "-c", "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')"], capture_output=True, text=True, check=True, timeout=10, stdin=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0 ) version = version_result.stdout.strip() environments.append({ "name": env_name, "path": python_path, "version": version }) except Exception: pass except Exception as e: print(f"Error getting conda environments: {e}", file=sys.stderr) return environments