{"record":{"id":"efacf622c07b3f86","repo":"pola-rs/polars","slug":"pfx-name-requires-self-module-name-r-module-to-be-installed","errorCode":null,"errorMessage":"{pfx}{name} requires {self._module_name!r} module to be installed","messagePattern":"(.+?)(.+?) requires (.+?) module to be installed","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_dependencies.py","lineNumber":104,"sourceCode":"\n        # accessing the proxy module's attributes triggers import of the real thing\n        if self._module_available:\n            # import the module and return the requested attribute\n            module = self._import()\n            return getattr(module, name)\n\n        # user has not installed the proxied/lazy module\n        elif name == \"__name__\":\n            return self._module_name\n        elif re.match(r\"^__\\w+__$\", name) and name != \"__version__\":\n            # allow some minimal introspection on private module\n            # attrs to avoid unnecessary error-handling elsewhere\n            return None\n        else:\n            # all other attribute access raises a helpful exception\n            pfx = self._mod_pfx.get(self._module_name, \"\")\n            msg = f\"{pfx}{name} requires {self._module_name!r} module to be installed\"\n            raise ModuleNotFoundError(msg) from None\n\n\ndef _lazy_import(module_name: str) -> tuple[ModuleType, bool]:\n    \"\"\"\n    Lazy import the given module; avoids up-front import costs.\n\n    Parameters\n    ----------\n    module_name : str\n        name of the module to import, eg: \"pyarrow\".\n\n    Notes\n    -----\n    If the requested module is not available (eg: has not been installed), a proxy\n    module is created in its place, which raises an exception on any attribute\n    access. This allows for import and use as normal, without requiring explicit\n    guard conditions - if the module is never used, no exception occurs; if it\n    is, then a helpful exception is raised.","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_dependencies.py#L86-L122","documentation":"When an optional dependency module is not installed, `_ModuleAvailableProxy.__getattr__` raises `ModuleNotFoundError` naming the attribute you accessed and the missing module (optionally with a package-name prefix for extras like `fsspec[http]`). It converts what would be an opaque `AttributeError` into an actionable install hint. Accessing `__path__`/availability checks returns None instead; all other attributes raise.","triggerScenarios":"Accessing any attribute of a not-installed optional module proxy, e.g. `polars.dependencies.xlsx_available` proxies or `mod.xlsxwrite...` calls when `xlsx2csv`/`fastexcel`/`pyarrow` etc. are absent; calling `df.write_excel()` without the xlsx extras installed and hitting the proxied import path.","commonSituations":"Fresh environments without optional extras (`pip install polars` instead of `polars[all]`); Docker images trimmed of optional deps; code paths for excel/databases/plotting that need `xlsx2csv`, `deltalake`, `fsspec`, `sqlalchemy`, etc.","solutions":["Install the missing module named in the message, e.g. `pip install fastexcel` or the matching extra `pip install 'polars[excel]'`.","Check availability first with `polars.dependencies.<name>_available` (or `_lazy_import`) before using optional features.","If you own the calling code, add the dependency to requirements/setup extras so environments are provisioned consistently."],"exampleFix":"// before\nimport polars as pl\ndf.write_excel('out.xlsx')  # ModuleNotFoundError: write_excel requires 'xlsxwriter'\n\n// after\n# pip install 'polars[excel]'\nimport polars as pl\ndf.write_excel('out.xlsx')","handlingStrategy":"fallback","validationCode":"import polars.dependencies as pdep\nif not pdep.fastexcel_available:\n    raise SystemExit('pip install fastexcel')","typeGuard":null,"tryCatchPattern":"try:\n    df.write_excel('out.xlsx')\nexcept ModuleNotFoundError as e:\n    logger.warning('optional dep missing, skipping export: %s', e)\n    # fallback: df.write_csv('out.csv')","preventionTips":["Install needed extras explicitly: `pip install 'polars[all]'` or per-feature extras.","Check `polars.dependencies.<mod>_available` before optional code paths.","Declare optional deps in your project's extras/requirements so environments are complete."],"tags":["python","polars","optional-dependency","modulenotfounderror","installation"],"backgroundTag":"missing-optional-dependency","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}