pola-rs/polars · error · ValueError

Invalid value for `POLARS_PREFER_PKG` variable

Error message

Invalid value for `POLARS_PREFER_PKG` variable: '{_prefer}'

What it means

POLARS_PREFER_PKG supplies an ordered preference of Rust extension packages Polars should try to load. If a listed name is not a key of the known pkgs dict, Polars raises ValueError with the invalid value. Unlike FORCE_PKG, an invalid PREFER value is not silently skipped — it is a hard configuration error.

Solutions

  1. Use only known package names from the pkgs dict in polars/_plr.py, separated per the documented format
  2. Unset the variable to fall back to the default preference order: `unset POLARS_PREFER_PKG`
  3. Print the current value (`echo $POLARS_PREFER_PKG`) and diff against valid names for your installed Polars
  4. Align Polars version with the source of the config if the name was valid for another version

Example fix

// before
$ export POLARS_PREFER_PKG=polars-uvloop  # ValueError: invalid value
// after
$ export POLARS_PREFER_PKG=polars-lts-cpu,polars
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_PREFER_PKG')
if v and any(name not in VALID for name in v.split(',')):
    raise SystemExit(f'POLARS_PREFER_PKG invalid: {v}')

Try / catch

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

Prevention

When it happens

Trigger: Exporting POLARS_PREFER_PKG with a name absent from the pkgs registry — typos, wrong separators, or names valid only in other Polars versions.

Common situations: CI config or Dockerfile ENV copied between projects with different Polars versions; guessing package names instead of copying from the registry; shell rc files accumulating stale exports.

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/01bf2918cfb4aa31. Report an issue: GitHub.

Appendix: source

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

    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__]
                    warnings.warn(
                        f"Skipping Polars' Rust module version '{sys.modules[__name__].__version__}' did not match version of Python package '{PKG_VERSION}'.",
                        ImportWarning,
                        stacklevel=2,
                    )
                    continue

                break

View on GitHub (pinned to fe841f959e)