{"record":{"id":"657d114540e17064","repo":"huggingface/open-r1","slug":"morphcloud-is-not-available-and-required-for-this","errorCode":null,"errorMessage":"MorphCloud is not available and required for this provider. Please install MorphCloud with `pip install morphcloud` and add an API key to a `.env` file.","messagePattern":"MorphCloud is not available and required for this provider\\. Please install MorphCloud with `pip install morphcloud` and add an API key to a `\\.env` file\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/open_r1/utils/code_providers.py","lineNumber":180,"sourceCode":"            finally:\n                try:\n                    await sandbox.kill()\n                except Exception as e:\n                    print(f\"Error from E2B executor kill with sandbox ID {sandbox.sandbox_id} : {e}\")\n\n\nclass MorphProvider(CodeExecutionProvider):\n    \"\"\"Provider that executes code using MorphCloud's Sandbox API.\"\"\"\n\n    def __init__(self, num_parallel: int = 2, morph_router_url: Optional[str] = None):\n        \"\"\"Initialize the Morph provider.\n\n        Args:\n            num_parallel: Number of parallel executions to use\n            morph_router_url: URL for the MorphCloud router (if using router mode)\n        \"\"\"\n        if not is_morph_available():\n            raise ImportError(\n                \"MorphCloud is not available and required for this provider. Please install MorphCloud with \"\n                \"`pip install morphcloud` and add an API key to a `.env` file.\"\n            )\n\n        try:\n            from dotenv import load_dotenv\n\n            load_dotenv()\n        except ImportError:\n            print(\"Warning: python-dotenv not installed. Environment variables must be set directly.\")\n\n        self.num_parallel = num_parallel\n        self.morph_router_url = morph_router_url\n\n        if self.morph_router_url is not None:\n            self.routed_sandbox = RoutedMorphSandbox(router_url=self.morph_router_url)\n            return\n","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/huggingface/open-r1/blob/1416fa0cf21595d2083b399a2a0bbddd7f6e9563/src/open_r1/utils/code_providers.py#L162-L198","documentation":"MorphProvider's __init__ raises ImportError when the morphcloud package is not importable. Like the E2B provider, it validates availability up front because remote sandbox execution is impossible without the SDK. It also expects a .env file with the MorphCloud API key for the later client setup.","triggerScenarios":"Instantiating MorphProvider (directly or through get_provider with a morph provider_type) while is_morph_available() returns False, i.e. the morphcloud import fails in the current interpreter.","commonSituations":"Deploying to a cluster node without the morphcloud extra installed; running in Docker where only base requirements were installed; using a different virtualenv than the one where morphcloud was pip-installed.","solutions":["Install the SDK: pip install morphcloud","Add MORPH_API_KEY to a .env file in the run's working directory (checked immediately after this guard)","Confirm availability in the same interpreter: python -c \"import morphcloud\"","Choose a different provider_type in get_provider if MorphCloud isn't intended"],"exampleFix":"// before\nprovider = get_provider(code_provider=\"morph\")  # ImportError\n// after\n# shell: pip install morphcloud && echo 'MORPH_API_KEY=...' >> .env\nprovider = get_provider(code_provider=\"morph\")","handlingStrategy":"validation","validationCode":"def require_morph():\n    try:\n        import morphcloud  # noqa: F401\n    except ImportError as e:\n        raise SystemExit(\"Install morphcloud: pip install morphcloud\") from e\n    import os\n    if not os.getenv(\"MORPH_API_KEY\"):\n        # .env is loaded by the provider; check file too\n        try:\n            with open(\".env\") as f:\n                assert any(l.startswith(\"MORPH_API_KEY=\") for l in f)\n        except (FileNotFoundError, AssertionError):\n            raise SystemExit(\"Set MORPH_API_KEY in .env or environment\")","typeGuard":"def is_morph_ready() -> bool:\n    try:\n        import morphcloud  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    provider = MorphProvider(num_parallel=4)\nexcept ImportError as e:\n    logger.error(\"MorphCloud missing: %s — run pip install morphcloud\", e)\n    raise SystemExit(1) from e","preventionTips":["Add morphcloud to the environment used by SLURM/Docker training images","Verify the import in the exact interpreter that runs the job","Keep a .env template (.env.example) listing MORPH_API_KEY","Fail fast with a preflight check before multi-hour jobs"],"tags":["python","missing-dependency","import-error","sandbox"],"backgroundTag":"missing-optional-dependency","analyzedSha":"1416fa0cf21595d2083b399a2a0bbddd7f6e9563","analyzedAt":"2026-08-30T08:56:53.400Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}