pola-rs/polars · error · ValueError

Invalid value for `POLARS_FORCE_PKG` variable

Error message

Invalid value for `POLARS_FORCE_PKG` variable: '{_force}'

What it means

Polars reads POLARS_FORCE_PKG to force a specific Rust extension package from a fixed dict of known packages. If the value is not a key of that dict, the lookup raises KeyError, which Polars converts into a ValueError explaining the invalid variable value.

Solutions

  1. Set POLARS_FORCE_PKG to an exact supported package name (e.g. `polars` or `polars-lts-cpu` — check the pkgs dict in polars/_plr.py for your version)
  2. Remove/unset the variable entirely to use automatic preference: `unset POLARS_FORCE_PKG`
  3. Verify the variable in the active environment: `echo $POLARS_FORCE_PKG` and inspect shell rc files
  4. If a wheel variant was dropped, upgrade or downgrade Polars to a version that still supports that package name

Example fix

// before
$ export POLARS_FORCE_PKG=polars-cpu  # ValueError: invalid value
// after
$ export POLARS_FORCE_PKG=polars-lts-cpu
Defensive patterns

Strategy: validation

Validate before calling

import os
VALID = {'polars', 'polars-lts-cpu', 'polars-u64-idx'}  # see pkgs dict in _plr.py
v = os.environ.get('POLARS_FORCE_PKG')
if v and v not in VALID:
    raise SystemExit(f'POLARS_FORCE_PKG invalid: {v}')

Try / catch

try:
    import polars as pl
except ValueError as e:
    os.environ.pop('POLARS_FORCE_PKG', None)
    import polars as pl

Prevention

When it happens

Trigger: Exporting POLARS_FORCE_PKG with a name not in the pkgs registry — typos, lowercase/uppercase variants, or a package name that no longer exists in the installed Polars version.

Common situations: Copying a POLARS_FORCE_PKG=... line from blog/docs for a different Polars version; typos like polars-lts-cpu= vs polars_lts_cpu; shell profiles exporting stale values.

Understand the failure class

Background: "is not a valid" / "Invalid ... value" environment variable errors: how libraries validate env vars and what to do when they reject yours — this error's family across 48 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/ec2d210d83404747. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_plr.py:65

    # Each of the Polars variants registers a `_polars...` package that we can import
    # the PLR from.

    _force = os.environ.get("POLARS_FORCE_PKG")
    _prefer = os.environ.get("POLARS_PREFER_PKG")

    pkgs = {"compat": rt_compat, "64": rt_64, "32": rt_32}
    default_prefer = [rt_compat, rt_64, rt_32]

    if _force is not None:
        try:
            pkgs[_force]()

            if sys.modules[__name__].__version__ != PKG_VERSION:
                msg = f"Polars Rust module for '{_force}' ({sys.modules[__name__].__version__}) did not match version of Python package '{PKG_VERSION}'"
                raise ImportError(msg)
        except KeyError:
            msg = f"Invalid value for `POLARS_FORCE_PKG` variable: '{_force}'"
            raise ValueError(msg) from None
    else:
        preference = default_prefer
        if _prefer is not None:
            try:
                preference.insert(0, pkgs[_prefer])
            except KeyError:
                msg = f"Invalid value for `POLARS_PREFER_PKG` variable: '{_prefer}'"
                raise ValueError(msg) from None

        version_warnings = []
        for pkg in preference:
            try:
                pkg()

                if sys.modules[__name__].__version__ != PKG_VERSION:
                    import warnings

                    version_warnings += [sys.modules[__name__].__version__]

View on GitHub (pinned to fe841f959e)