{"record":{"id":"96d0a3d5ae0a46d6","repo":"huggingface/transformers","slug":"this-modeling-file-requires-the-following-packages","errorCode":null,"errorMessage":"This modeling file requires the following packages that were not found in your environment: {', '.join(missing_packages)}. Run `pip install {' '.join(missing_packages)}`","messagePattern":"This modeling file requires the following packages that were not found in your environment: (.+?)\\. Run `pip install (.+?)`","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"src/transformers/dynamic_module_utils.py","lineNumber":258,"sourceCode":"        `list[str]`: The list of relative imports in the file.\n    \"\"\"\n    imports = get_imports(filename)\n    missing_packages = []\n    for imp in imports:\n        try:\n            importlib.import_module(imp)\n        except ImportError as exception:\n            logger.warning(f\"Encountered exception while importing {imp}: {exception}\")\n            # Some packages can fail with an ImportError because of a dependency issue.\n            # This check avoids hiding such errors.\n            # See https://github.com/huggingface/transformers/issues/33604\n            if \"No module named\" in str(exception):\n                missing_packages.append(imp)\n            else:\n                raise\n\n    if len(missing_packages) > 0:\n        raise ImportError(\n            \"This modeling file requires the following packages that were not found in your environment: \"\n            f\"{', '.join(missing_packages)}. Run `pip install {' '.join(missing_packages)}`\"\n        )\n\n    return get_relative_imports(filename)\n\n\ndef get_class_in_module(\n    class_name: str,\n    module_path: str | os.PathLike,\n    *,\n    force_reload: bool = False,\n) -> type:\n    \"\"\"\n    Import a module on the cache directory for modules and extract a class from it.\n\n    Args:\n        class_name (`str`): The name of the class to import.","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/dynamic_module_utils.py#L240-L276","documentation":"When transformers copies a remote (trust_remote_code) modeling file into its dynamic-module cache, it first imports the file's top-level dependencies. Dependencies that fail with 'No module named ...' are collected and reported as one ImportError naming all missing packages and the exact pip install command; other ImportErrors (broken dependency, not missing) are re-raised unchanged.","triggerScenarios":"Loading a custom-code model (AutoModel.from_pretrained(some_community_model, trust_remote_code=True)) whose modeling .py imports e.g. einops, tiktoken, or sentencepiece that are absent from the current environment.","commonSituations":"New machine/venv without the model's extras; Colab notebooks missing einops; a model repo added a new dependency after you first used it.","solutions":["Run the pip install command shown verbatim in the message: pip install <packages>.","Add those packages to your requirements/environment.yml so redeploys do not regress.","If the install fails, check for version conflicts reported by pip."],"exampleFix":"# before\nAutoModel.from_pretrained(\"org/custom-model\", trust_remote_code=True)  # ImportError: requires einops, tiktoken\n\n# after\npip install einops tiktoken\nAutoModel.from_pretrained(\"org/custom-model\", trust_remote_code=True)","handlingStrategy":"try-catch","validationCode":"def missing_pkgs(imports: list[str]) -> list[str]:\n    import importlib.util\n    return [m for m in imports if importlib.util.find_spec(m) is None]","typeGuard":null,"tryCatchPattern":"try:\n    AutoModel.from_pretrained(repo_id, trust_remote_code=True)\nexcept ImportError as e:\n    if \"were not found in your environment\" in str(e):\n        pkgs = str(e).split('pip install ')[1].rstrip('.')\n        subprocess.check_call([sys.executable, '-m', 'pip', 'install', *pkgs.split()])\n        AutoModel.from_pretrained(repo_id, trust_remote_code=True)\n    else:\n        raise","preventionTips":["Pre-install a model's declared dependencies when onboarding it.","Treat the pip command embedded in the message as the source of truth.","Snapshot working environments (pip freeze) before adopting new remote-code models."],"tags":["dependencies","trust-remote-code","environment","import"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}