{"record":{"id":"3b3e8c391ced3be8","repo":"pola-rs/polars","slug":"polars-rust-module-for-force-sys-modules-name-version-did","errorCode":null,"errorMessage":"Polars Rust module for '{_force}' ({sys.modules[__name__].__version__}) did not match version of Python package '{PKG_VERSION}'","messagePattern":"Polars Rust module for '(.+?)' \\((.+?)\\) did not match version of Python package '(.+?)'","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_plr.py","lineNumber":62,"sourceCode":"if hasattr(builtins, \"__POLARS_PLR\"):\n    sys.modules[__name__] = builtins.__POLARS_PLR\nelse:\n    # Each of the Polars variants registers a `_polars...` package that we can import\n    # the PLR from.\n\n    _force = os.environ.get(\"POLARS_FORCE_PKG\")\n    _prefer = os.environ.get(\"POLARS_PREFER_PKG\")\n\n    pkgs = {\"compat\": rt_compat, \"64\": rt_64, \"32\": rt_32}\n    default_prefer = [rt_compat, rt_64, rt_32]\n\n    if _force is not None:\n        try:\n            pkgs[_force]()\n\n            if sys.modules[__name__].__version__ != PKG_VERSION:\n                msg = f\"Polars Rust module for '{_force}' ({sys.modules[__name__].__version__}) did not match version of Python package '{PKG_VERSION}'\"\n                raise ImportError(msg)\n        except KeyError:\n            msg = f\"Invalid value for `POLARS_FORCE_PKG` variable: '{_force}'\"\n            raise ValueError(msg) from None\n    else:\n        preference = default_prefer\n        if _prefer is not None:\n            try:\n                preference.insert(0, pkgs[_prefer])\n            except KeyError:\n                msg = f\"Invalid value for `POLARS_PREFER_PKG` variable: '{_prefer}'\"\n                raise ValueError(msg) from None\n\n        version_warnings = []\n        for pkg in preference:\n            try:\n                pkg()\n\n                if sys.modules[__name__].__version__ != PKG_VERSION:","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_plr.py#L44-L80","documentation":"When POLARS_FORCE_PKG selects a specific Rust extension package, Polars verifies that the loaded Rust module's version equals the Python package version (PKG_VERSION). A mismatch means the wheel set is inconsistent (Python and Rust halves out of sync), so an ImportError is raised rather than risking ABI/behavior breakage.","triggerScenarios":"Setting env var POLARS_FORCE_PKG=<name> (e.g. polars-lts-cpu, polars-u64-idx) where the selected Rust package's compiled version differs from the installed polars Python package version; mixing releases during upgrade/downgrade; stale cached wheels after `pip install -U`.","commonSituations":"Upgrading polars without upgrading its companion wheel (or vice versa); partial installs in monorepo lockfiles; pinning a force-PKG variable in CI that resolves to a lagging package version.","solutions":["Reinstall the matched set in one command: `pip install -U polars polars-lts-cpu` (or the forced package name)","Check `pip list | grep polars` and align every polars* package to the same version","Remove the POLARS_FORCE_PKG env var if the specific wheel variant is not actually required","Clear the environment/uv/pip cache and reinstall if versions appear equal but the error persists"],"exampleFix":"// before\n$ POLARS_FORCE_PKG=polars-lts-cpu python -c 'import polars'  # ImportError: version mismatch\n// after\n$ pip install -U polars polars-lts-cpu\n$ python -c 'import polars'","handlingStrategy":"validation","validationCode":"import os, importlib.metadata as md\nforce = os.environ.get('POLARS_FORCE_PKG')\nif force:\n    v_py = md.version('polars')\n    v_ext = md.version(force) if force != 'polars' else v_py\n    assert v_py == v_ext, f'{force} {v_ext} != polars {v_py}'","typeGuard":null,"tryCatchPattern":"try:\n    import polars as pl\nexcept ImportError:\n    os.environ.pop('POLARS_FORCE_PKG', None)\n    import polars as pl","preventionTips":["Always upgrade polars and its companion wheels in a single command","Avoid exporting POLARS_FORCE_PKG globally; scope it to the shell/job that needs it","Run `pip list | grep polars` in CI to verify version alignment","Keep the force variable in infrastructure-as-code, not ad-hoc exports"],"tags":["python","installation","version-conflict","environment-variable"],"backgroundTag":"version-mismatch","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"}